Sql 简单JSON数组到行

Sql 简单JSON数组到行,sql,sql-server,tsql,sql-server-2016,Sql,Sql Server,Tsql,Sql Server 2016,我想获取以下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,

我想获取以下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.

你把路放错地方了。试试这个

select * from openjson(@com,'strict $.IDs')

返回的集合包括元素的位置(在
键中)及其值和类型。

如果有人想加入MySQL中JSON数组中的字段,下面是一个使用FIND_in_set将ID数组加入另一个表的查询

它的性能不好,但它可以工作

(MySQL 5.7不支持REGEXP_REPLACE)

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"
    ]
}
语法
with(商品varchar(50)'strict$.IDs')
要求提供数组索引,例如
with(商品varchar(50)'strict$.IDs[0]')
。但是,-Shnugo正确地回答了这个问题,我相信是为了提取数组中的所有值。
    "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"
    ]
}