I have a JSON array that looks like this
[
{
"_id": "12345",
"uniqueId": null,
"companyName": "ABC Corp",
"yearFounded": 1958,
"stateFounded": "Delaware",
"someField": [
{
"primaryCode": "14",
"secondaryCode": "32",
"tertiaryCode": "00",
"description": "Moving Walks"
},
{
"primaryCode": "14",
"secondaryCode": "40",
"tertiaryCode": "00",
"description": "Lifts"
},
{
"primaryCode": "14",
"secondaryCode": "00",
"tertiaryCode": "00",
"description": "Conveying Equipment"
}
],
"SomeRegionProperty": [
{
"region": "Other"
},
{
"region": "MD - Eastern Shore"
},
{
"region": "MD - Southern"
},
{
"region": "MD - Central (incl. Baltimore)"
}
],
"Markets": [
{
"market": "Pharmaceutical & Laboratory"
},
{
"market": "Retail"
}
],
"SomeEmptyProperty": [],
"AndYetAnother": [
{
"unionName": "Name",
"unionNumber": "Value 1234"
}
]
},
{
"_id": "949690",
"companyName": "XYZ Co",
"yearFounded": 2015,
"stateFounded": "New York",
"someField": [
{
"primaryCode": "15",
"secondaryCode": "62",
"tertiaryCode": "032",
"description": "test"
}
],
"SomeRegionProperty": [
{
"region": "Other"
},
{
"region": "MD - Eastern Shore"
},
{
"region": "MD - Southern"
},
{
"region": "MD - Central (incl. Baltimore)"
}
],
"Markets": [
{
"market": "Pharmaceutical & Laboratory"
},
{
"market": "Retail"
}
],
"SomeEmptyProperty": [],
"AndYetAnother": [
{
"unionName": "Name",
"unionNumber": "Value 1234"
}
]
}
]
So, I need to distribute the object into sql tables (sql server) With the table "MainTbl" - with the Outer most level fields as columns and _id as primary key. THen tables for each nested part ("SomField", "SomeRegionProperty", "Markets" .. etc..) with _id being a foreign key in those tables.
I understand that openJson returns a table value. But, how can I preserve _id=12345 while inserting 3 records for "someField ? etc...
I want to end up with structure like this
And my JSON can be very large, there can be many related tables with many fields. So, going through OPENJSON
on the main input multiple times is not too desirable. It may slow down the process.
But, I will take it if there is no other way.