I have the following json in a column:
{
"fields":[
{
"field":"modelName",
"value":"abc123"
},
{
"field":"displayName",
"value":"ABC 123"
},
{
"field":"order",
"value":5
}
],
"variables":[
{
"varId":4,
"oldValue":100,
"newValue":"150"
},
{
"varId":5,
"oldValue":"abc",
"newValue":"def"
}
]
}
And I would like to pull this information out to something like the following:
Id Field Value VarId oldValue newValue
2 modelName abc123 null null null
2 displayName ABC 123 null null null
2 order 5 null null null
2 null null 4 100 150
2 null null 5 abc def
That way I can just iterate through the result set and just null check to see what type it is.
I currently have the following statement:
select Id, Fields.Field, Fields.Value, Variables.VarId, Variables.OldValue, Variables.NewValue from Product
cross apply openjson( data, '$.fields') with (Field varchar(50) '$.field', Value varchar(50) '$.value') AS Fields
cross apply openjson( data, '$.variables') with (VarId int '$.varId', OldValue varchar(50) '$.oldValue', NewValue varchar(50) '$.newValue') AS Variables
But it gives me the following output:
As you can see, everything is duplicated. Is it possible to get the output that I wanted?
Thanks
