How to parse nested JSON array using SQL Server

后端 未结 2 1554
孤城傲影
孤城傲影 2021-02-13 11:02

I am currently able to parse most of a JSON file using SQL Server\'s \"OPENJSON WITH (...\" syntax. However, this particular file contains nested arrays which I do not know how

2条回答
  •  [愿得一人]
    2021-02-13 11:22

    After MUCH searching I finally discovered the answer to this issue. I simply have to include the Nested Array as another JSON column in my query, like:

    WITH
     (DOC_ID                varchar(100)  '$.doc._id',
      DOC_REV               varchar(45)   '$.doc._rev',
      DELY_APPL_NAME        varchar(20)   '$.doc.delivery.application',
      DELY_SENT_BY          varchar(25)   '$.doc.delivery.sender.id',
      DELY_SENT_TYPO        varchar(20)   '$.doc.delivery.sender.type',
      RECIPS                nvarchar(max) '$.doc.delivery.recipients' as JSON,
      PAYLOAD_START_TIME    varchar(30)   '$.doc.payload.startTS',
      ....
      ...
      ..
      ) as my_query
    

    So, I end up with one record per JSON document, with (in this case) a varchar column containing JSON text.

    Next, I can run a separate query on this column to parse the JSON and create a "child table" associated with the Parent.

提交回复
热议问题