简体   繁体   中英

Parse JSON Nested Arrays in SQL

This is a question which I had a couple days ago for which I couldn't find an answer online but was able to find some fundamentals and build on them.

I am posting it here in the hope it is of use to anybody else with a similar issue and because it was a piece of code I really enjoyed learning.

Background:

We receive data from a downstream system in JSON format and then use SSIS to lannd this data into out SQL Server instance as 3 columns:

JSONSTRING NVARCHAR(MAX)
IS_VALID_JSON INT
RECORD_IDENTIFIER Computed NVARCHAR(4000)

We are in the process of creating views which parse the JSON Data to create Landing views to enable further transformations and interrogations of the data.

Problem:

Within the parsing of the data I had instances of nested arrays, as per the following where the rules data is stored as an array inside of another array:

{
  "system": {
    "interchange": {
      "received_date": "2018-05-01",
      "schema_version": "ABC123"
    },
    "record": {
      "identifier": "1525165200000999999",
      "version": "20180716220047900"
    },
    "clinical": {
      "procedures": {
        "read": [
          {
            "code": "7516C",
            "code_cleansed": {
              "value": "7516C",
              "was_changed": true,
              "rules": [
                {
                  "rule": "7357"
                },
                {
                  "rule": "TEST"
                }
              ]
            },
            "code_3": "751"
          }
        ]
      }
    }
  }
}

Within my view therefore I needed to access both the read[] and rules[] array's.

I was experimenting the basic code examples provided by MS and here is what I arrived at that has done the job:

SELECT
JSON3.[RECORD_IDENTIFIER]
,[EMERGENCY CARE PROCEDURE (SNOMED CT)]
,[PRIORITY TYPE CODE]
,[PROCEDURE_CODE_WAS_CHANGED]
,JSON_VALUE (JSON3.[RULES_ARRAY], '$.rule') AS [PROCEDURE_CODE_WAS_CHANGED_RULE]
,[PROCEDURE_CODE_FIRST_3_CHAR]
,[PROCEDURES_CODE_FIRST_4_CHAR]
,[PROCEDURE DATE]

FROM
    (
    SELECT
        [RECORD_IDENTIFIER]
        ,JSON_VALUE ([PROCEDURES_ARRAY], '$.code') AS [EMERGENCY CARE PROCEDURE (SNOMED CT)]
        ,JSON_VALUE ([PROCEDURES_ARRAY], '$.code_cleansed.value') AS [PRIORITY TYPE CODE]
        ,JSON_VALUE ([PROCEDURES_ARRAY], '$.code_cleansed.was_changed') AS [PROCEDURE_CODE_WAS_CHANGED]
        ,JSON_VALUE ([PROCEDURES_ARRAY], '$.code_3') AS [PROCEDURE_CODE_FIRST_3_CHAR]
        ,JSON_VALUE ([PROCEDURES_ARRAY], '$.date') AS [PROCEDURE DATE]
        ,value AS [RULES_ARRAY]
    FROM
            (
            SELECT
                [RECORD_IDENTIFIER],
                value AS [PROCEDURES_ARRAY]
            FROM
                [LND].[OP_JSON_REPORT_LAND]
                 CROSS APPLY OPENJSON ([JSONSTRING],'$.clinical.procedures.read')
            ) AS JSON CROSS APPLY OPENJSON ([PROCEDURES_ARRAY], '$.code_cleansed.rules') AS JSON2
    ) AS JSON3

I hope this is of help to someone, thanks for reading.

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