This is an awkward thing to word, so if this has been asked before sorry, I couldn't find any answers.
I'm taking JSON which is a list of objects and storing the fields in a table, however the JSON is from a third party and is prone to adding in new fields, so I'd like to also save the raw JSON of the object. So that if any unhandled fields were added we have the raw JSON, and can update the DB for those fields and run through the stored JSON, so no data is lost.
I can't however find or figure out how to just get the raw JSON of the current list item.
Basic OPENJSON I have:
DECLARE @JSON NVARCHAR(MAX) = '{"data": [
{ "id": 1,
"name": "foo",
"type": "bar",
"random": "Potato"},
{ "id": 2,
"name": "cake",
"type": "special",
"random": "unhandled field"}]}'
SELECT *
FROM OPENJSON(@JSON, '$.data')
WITH (
[id] INT '$.id',
[type] NVARCHAR(50) '$.type',
[name] NVARCHAR(50) '$.name'
) jsonData
but I can't find anywhere documenting how to also as a field get the entire JSON for the item.
My ideal tabled output being:
| id | name | type | json |
|---|---|---|---|
| 1 | foo | bar | { "id": 1,"name": "foo", "type": "bar", "random": "Potato"} |
| 2 | cake | special | { "id": 2, "name": "cake", "type": "special", "random": "unhandled field"} |
[json] NVARCHAR(MAX) '$' AS JSONto yourWITH.