简体   繁体   中英

Format JSON as nested arrays based on id in SQL

I have a stored procedure in SQLS erver that gives the output in the following JSON format:

[
   {
  "accountid":"12312312",
  "Id":1,
  "name":"Ace Signs Ltd"
   },
   {
  "accountid":"213123123",
  "Id":2,
  "name":"Workshare Technology"
   },
  {
  "accountid":"12312312",
  "Id":1,
  "name":"Ace Signs Ltd"
  },
  {
  "accountid":"123123123",
  "Id":2,
  "name":"Workshare"
   }
]

But I want them grouped into nested arrays based on the ID as shown below. Where the ones with the same ID are one tuple.

   {
   "match":[
      [
        {
        "accountid":"12312312",
        "Id":1,
        "name":"Ace Signs Ltd"
        },
        {
        "accountid":"12312312",
        "Id":1,
        "name":"Ace Signs Ltd"
        }
      ],
      [
        {
        "accountid":"12312312",
        "Id":2,
        "name":"Workshare Technology"
        },
        {
        "accountid":"213123123",
        "Id":2,
        "name":"Workshare"
        }
       ]
      ]
    }

The part of the stored procedure that generates the JSON is as follows:

SELECT *
FROM TestJsonFormat 
FOR JSON AUTO

Sample data:

CREATE TABLE TestJsonFormat (
    accountid varchar(255), 
    Id int,
    name varchar(255)
)

insert into TestJsonFormat values(12312312,1,'Ace Signs Ltd')
insert into TestJsonFormat values(213123123,2,'Workshare Technology')
insert into TestJsonFormat values(12312312,1,'Ace Signs Ltd')
insert into TestJsonFormat values(123123123,2,'Workshare')

One possible approach to generate the expected JSON output is a combination from FOR JSON AUTO and basic string aggregation. The following example demostrates this:

Table:

CREATE TABLE TestJsonFormat (
    accountid varchar(255), 
    Id int,
    name varchar(255)
)
insert into TestJsonFormat values(12312312,1,'Ace Signs Ltd')
insert into TestJsonFormat values(213123123,2,'Workshare Technology')
insert into TestJsonFormat values(12312312,1,'Ace Signs Ltd')
insert into TestJsonFormat values(123123123,2,'Workshare')

Statement:

SELECT CONCAT('{"match": [', STRING_AGG(json, ','), ']}')
FROM (
   SELECT DiSTINCT t.id, j.json
   FROM TestJsonFormat t
   CROSS APPLY (
      SELECT accountId, id, name
      FROM TestJsonFormat 
      WHERE id = t.id
      FOR JSON AUTO
   ) j (json)
) cte

Result (formatted):

{
  "match": [
    [
      {
        "accountId":"12312312",
        "id":1,
        "name":"Ace Signs Ltd"
      },
      {
        "accountId":"12312312",
        "id":1,
        "name":"Ace Signs Ltd"
      }
    ],
    [
      {
        "accountId":"213123123",
        "id":2,
        "name":"Workshare Technology"
      },
      {
        "accountId":"123123123",
        "id":2,
        "name":"Workshare"
      }
    ]
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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