I have data like below in a file which I read into a variable in SQL Server:
{
"TypeCode": [
{
"DestinationValue": "Private",
"SourceValue": "1"
},
{
"DestinationValue": "Public",
"SourceValue": "2"
}
],
"TypeDesc": [
{
"DestinationValue": "Hello",
"SourceValue": "1"
},
{
"DestinationValue": "Bye",
"SourceValue": "2"
}
]
}
Now I need to convert this into data like below:
Name SourceValue DestValue
--------------------------------------------
TypeCode 1 Hello
TypeCode 2 Bye
Concern is - there can be many such arrays in this JSON and code should be automatically able to handle all of them without changing the code.
How can we achieve this?
You need to parse the input JSON using
OPENJSON()
twice (with default and explicit schema):Result: