我想采用以下JSON并将其转换为数组
declare @com nvarchar(MAX)
set @com = '{"IDs":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,83,84,88,89,90,91,97,98,99,100,101,102,104,108,109,110,111,112,114,115,116,118,119,121,122,123,124,125,126,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,152,153,154]}';
select isJson(@com)
select * from openjson(@com)
with (commodities varchar(50) 'strict $.IDs')
但是当我收到以下错误消息时,我在做什么错了?
Object or array cannot be found in the specified JSON path.
答案 0 :(得分:2)
您将路径放置在错误的位置。试试这个
select * from openjson(@com,'strict $.IDs')
返回的集合包括元素的位置(在key
中)及其值和类型。
答案 1 :(得分:0)
如果有人希望加入 MySQL 中 JSON 数组中的字段,这里是一个使用 FIND_IN_SET 将 ID 数组加入另一个表的查询。
它表现不佳,但有效。
(MySQL 5.7 不支持 REGEXP_REPLACE)
select c.classId, c.className, replace(replace(replace(replace(sc.classes->>'$.classIds','"',''),'[',''),']',''),' ','')
from studentClasses sc
join classes c on find_in_set(c.classId,replace(replace(replace(replace(sc.classes->>'$.classIds','"',''),'[',''),']',''),' ',''))
where 1
limit 10;
classes JSON 列如下所示:
"classIds": [
"d4ae08d0-c27c-11ea-87f0-3508520d9867",
"556c3060-d0f5-11ea-995d-9709c7e03f55",
"558eac80-d0f5-11ea-995d-9709c7e03f55",
"559192b0-d0f5-11ea-995d-9709c7e03f55",
"d57613c0-c27c-11ea-87f0-3508520d9867",
"d551e9f0-c27c-11ea-87f0-3508520d9867",
"d4b44a60-c27c-11ea-87f0-3508520d9867"
]
}