I'm trying to parse a string that contains X amount of the same JSON nodes, concatenated together:
declare @jsonData varchar(max) =
'{
"data":{
"identity":1,
"elementValue": "300833b2ddd9014000000000"
},
"timestamp":"2023-04-25T00:56:57.891+0000"
}
{
"data":{
"identity":2,
"elementValue": "ad72120643eb1fb1867c005c"
},
"timestamp":"2023-04-25T00:56:57.898+000"
}'
I'd like to parse the string and return the values within the 'data' node:
identity | elementValue |
---|---|
1 | 300833b2ddd9014000000000 |
2 | ad72120643eb1fb1867c005c |
I believe this is possible with OPENJSON() but I'm struggling with the CROSS APPLY syntax- any help appreciated, thanks!
Your JSON is actually invalid. It has no surrounding array brackets and no comma in between objects. I've assumed your actual data is correct.
This is pretty straightforward using a single OPENJSON
to shred the array into values, and use the path syntax to specify how to retrieve each property.
declare @jsonData nvarchar(max) =
'[{
"data":{
"identity":1,
"elementValue": "300833b2ddd9014000000000"
},
"timestamp":"2023-04-25T00:56:57.891+0000"
},
{
"data":{
"identity":2,
"elementValue": "ad72120643eb1fb1867c005c"
},
"timestamp":"2023-04-25T00:56:57.898+000"
}
]
';
select *
from OPENJSON(@jsonData)
with (
[identity] int '$.data.identity',
elementValue nvarchar(100) '$.data.elementValue'
) as j;
But if you really want to use cross apply
or you have other higher-level elements to retrieve then you can feed the result of one OPENJSON
into the next. You need to specify as json
to retrieve properties as whole objects or arrays without parsing them.
select j2.*
from OPENJSON(@jsonData)
with (
data nvarchar(max) as json
) as j1
cross apply OPENJSON(j1.data)
with (
[identity] int,
elementValue nvarchar(100)
) as j2;