简体   繁体   English

SQL 中的 CONCAT JSON 数组值

[英]CONCAT JSON array values in SQL

I'm trying to parse some JSON data in SQL.我正在尝试解析 SQL 中的一些 JSON 数据。 In the following JSON string I have an array called "val" which has multiple elements.在下面的 JSON 字符串中,我有一个名为“val”的数组,它有多个元素。


declare @JSONVariable nvarchar(max)
select @JSONVariable  = N'{"name":"North Carolina","region":"North East",
"category":[
    {"group_id":1AB,
        "groups":[{"val":[3242,4234],"make":{"type":"US","value":"1235"}}]},
    {"group_id":1AC,
        "groups":[{"val":[2354],"make":{"type":"US","value":"2342"}}]},
    {"group_id":1CS,
        "groups":[{"val":[242,3433,2424],"make":{"type":"US","value":"0656379"}}]},
    {"group_id":3AC,
        "groups":[{"val":[2463,4633],"make":{"type":"US","value":"3453"}}]}
    ]}'

I want to get all the elements in "Val" array as 1 comma separated string.我想将“Val”数组中的所有元素作为 1 个逗号分隔的字符串。 Below is the query I wrote and I can only get the 1st element of the val array.下面是我写的查询,我只能得到 val 数组的第一个元素。 How can I combine all the elements to a comma separated string?如何将所有元素组合成逗号分隔的字符串?


SELECT a.*, aa.group_id, aa.val,aa.make_type,aa.make_value
FROM OPENJSON(@JSONVariable, '$.category') a
CROSS APPLY OPENJSON(aa.value)
WITH (    
        group_id nvarchar(100) '$.group_id',
        val nvarchar(max)  '$.groups[0].val[0]', -- need all the values in val array comma separated
        make_type nvarchar(10) '$.groups[0].make.type',
        make_value nvarchar(100) '$.groups[0].make.value'
    ) aa

If we can assume you have valid JSON, then you simply need to remove the positional operator from val :如果我们可以假设您有有效的 JSON,那么您只需从val中删除位置运算符:


Declare @JSONVariable nvarchar(max)
select @JSONVariable  = N'{"name":"North Carolina","region":"North East",
"category":[
    {"group_id":"1AB",
        "groups":[{"val":[3242,4234],"make":{"type":"US","value":"1235"}}]},
    {"group_id":"1AC",
        "groups":[{"val":[2354],"make":{"type":"US","value":"2342"}}]},
    {"group_id":"1CS",
        "groups":[{"val":[242,3433,2424],"make":{"type":"US","value":"0656379"}}]},
    {"group_id":"3AC",
        "groups":[{"val":[2463,4633],"make":{"type":"US","value":"3453"}}]}
    ]}';



SELECT c.group_id,
       c.val,
       c.make_type,
       c.make_value
FROM OPENJSON(@JSONVariable, '$.category') 
WITH (group_id nvarchar(100) '$.group_id',
      val nvarchar(max)  '$.groups[0].val' AS JSON, --Should these groups also only be position 0?
      make_type nvarchar(10) '$.groups[0].make.type',
      make_value nvarchar(100) '$.groups[0].make.value') c --aa for category doesn't make much sense. We'll go with c for category

This brings the value back as a JSON array, rather than as a comma separated value.这会将值返回为 JSON 数组,而不是逗号分隔值。 If you need to have it as a just CSV, you simply need to replace the brakets;如果你需要它作为一个只是 CSV,你只需要更换刹车; which can easily be done with a couple of nested REPLACE s.这可以通过几个嵌套的REPLACE轻松完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM