I am have sample JSON as below
{
"12345": {
"percent": 26,
"avgPrice": 32,
"avgNewOfferCount": 12,
"avgUsedOfferCount": -1,
"isFBA": true,
"lastSeen": 653
}
}
I am getting the array values but since my array head i.e. '12345' is not fixed but an ID I am not able to get it and I can't hardcode since ID will change.
I am trying as below:
SELECT @json j;
INSERT INTO [test].[test]
SELECT
JSON_VALUE(j.[value],$.j) AS key,
JSON_VALUE(j.[value],'$.percent') AS stat
FROM OPENJSON(@json) j;
But getting NULL in the key column.
Your query would have worked by simply selecting
j.[key].But you are better off just using two levels of
OPENJSON, one feeding into the next.The first does not use a schema, so you get a list of
keyvaluepairs.keyrepresents the property name, which in this case would be12345. Then you push thevalueinto the next call.db<>fiddle