简体   繁体   中英

Advice on using OPENJSON to parse a nested array

I am hoping somebody can help point me in the right direction as I'm trying to parse a json file into sql using OPENJSON. I have a structure which looks like this:

DECLARE @json AS NVARCHAR(MAX) = '

[{
        "id": "78",
        "Version": {
            "Value": "12"
        },

        "Names": [{
                "NameId": {
                    "Value": "8516365"
                },
                "id": "328787",
                "NameLinkType": {
                    "Value": "A"

                    "CommsChains": {
                        "Value": [[{
                                    "com_primary": {
                                        "Value": "Y"
                                    },
                                    "com_recd": {
                                        "Value": "2020-07-07 00:00:00.000"
                                    },
                                    "com_ack": {
                                        "Value": "2020-07-09 00:00:00.000"

                                    },
                                }
                            ]]
                ),  },
            },
                    
        ],
        
    }
]'

I am able to parse the majority of the JSON correctly, so for each ID I can return values such as Version or NameId . However, I am unable to return any dates in respect of com_recd or com_ack , which sit under CommsChains [Object] – Value [Array] – [0] [Array]

在此处输入图像描述

It looks like there are some syntactic errors in your JSON. After having them fixed, I was able to try and find the JSON paths to the date expressions to the date values. This is the SQL:

DECLARE @json AS NVARCHAR(MAX) = '
[{
        "id": "78",
        "Version": {
            "Value": "12"
        },

        "Names": [{
                "NameId": {
                    "Value": "8516365"
                },
                "id": "328787",
                "NameLinkType": {
                    "Value": "A",

                    "CommsChains": {
                        "Value": [[{
                                    "com_primary": {
                                        "Value": "Y"
                                    },
                                    "com_recd": {
                                        "Value": "2020-07-07 00:00:00.000"
                                    },
                                    "com_ack": {
                                        "Value": "2020-07-09 00:00:00.000"

                                    }
                                }
                            ]]
                    }
                }
            }
                    
        ]
        
    }
]'
select * from openjson(@json, '$[0].Version');--Value 12 1
select * from openjson(@json, '$[0].Names');
select * from openjson(@json, '$[0].Names[0]');
select * from openjson(@json, '$[0].Names[0].NameLinkType');
select * from openjson(@json, '$[0].Names[0].NameLinkType.CommsChains');
select * from openjson(@json, '$[0].Names[0].NameLinkType.CommsChains.Value');
select * from openjson(@json, '$[0].Names[0].NameLinkType.CommsChains.Value[0]');
select * from openjson(@json, '$[0].Names[0].NameLinkType.CommsChains.Value[0][0]');

select * from openjson(@json, '$[0].Names[0].NameLinkType.CommsChains.Value[0][0].com_recd'); --selecting path for com_recd
select * from openjson(@json, '$[0].Names[0].NameLinkType.CommsChains.Value[0][0].com_ack');  --selecting path for com_ack

Herein I show the selects to the different parts of your JSON. The arrays'content are always referenced as [0] as its always the first index to select here.

For more information on JSON paths on the SQL server look here

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