Parse and Store Parent/Child JSON Across Multiple Tables
up vote
1
down vote
favorite
This link json part 3 open json shows how to shred json into parent child records.
Specifically showing how to generate multiple result sets from one Json Document
[
"CharacterID" : null,
"CharacterName" : "Egwene Al''Vere",
"IsFemale" : true,
"Address" :
"AddressLine1" : "Emonds Field",
"AddressLine2" : "The Two Rivers"
,
"Friends" : ["Rand Al''Thor", "Perrin Aybara", "Matrim Cauthon"],
"Children" : null
,
"CharacterID" : null,
"CharacterName" : "Second Character",
"IsFemale" : false,
"Address" :
"AddressLine1" : "Emonds Field 2",
"AddressLine2" : "The Three Rivers"
,
"Friends" : ["Jeff", "Zeke", "Sid Cauthon"],
"Children" : null
]
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
JSON_VALUE(@json, '$.CharacterName') AS CharacterName,
JSON_VALUE(@json, '$.IsFemale') AS IsFemale,
JSON_VALUE(@json, '$.Address.AddressLine1') AS AddressLine1,
JSON_VALUE(@json, '$.Address.AddressLine2') AS AddressLine2,
JSON_VALUE(@json, '$.Children') AS Children
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
OJ.value AS Friend
FROM OPENJSON(@json, '$.Friends') OJ;
I would like to like to write a procedure to insert this document into a parent Character Table, and a child CharacterFriend table, maintaining FK references.
If the ID was not provided it would be fairly easy to figure out what it was using SCOPE IDENTITY, and then filling that in before you inserted into the CharacterFriend Table.
However, it is less clear to me how you would discover the PK of the Character table in the event that there are multiple characters defined in the json document.
What would be the best way to write an insert procedure that establishes this FK in this case. The only thing that's jumping out at me is a cursor, but this feels wrong.
json sql-server sql-server-2016
add a comment |
up vote
1
down vote
favorite
This link json part 3 open json shows how to shred json into parent child records.
Specifically showing how to generate multiple result sets from one Json Document
[
"CharacterID" : null,
"CharacterName" : "Egwene Al''Vere",
"IsFemale" : true,
"Address" :
"AddressLine1" : "Emonds Field",
"AddressLine2" : "The Two Rivers"
,
"Friends" : ["Rand Al''Thor", "Perrin Aybara", "Matrim Cauthon"],
"Children" : null
,
"CharacterID" : null,
"CharacterName" : "Second Character",
"IsFemale" : false,
"Address" :
"AddressLine1" : "Emonds Field 2",
"AddressLine2" : "The Three Rivers"
,
"Friends" : ["Jeff", "Zeke", "Sid Cauthon"],
"Children" : null
]
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
JSON_VALUE(@json, '$.CharacterName') AS CharacterName,
JSON_VALUE(@json, '$.IsFemale') AS IsFemale,
JSON_VALUE(@json, '$.Address.AddressLine1') AS AddressLine1,
JSON_VALUE(@json, '$.Address.AddressLine2') AS AddressLine2,
JSON_VALUE(@json, '$.Children') AS Children
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
OJ.value AS Friend
FROM OPENJSON(@json, '$.Friends') OJ;
I would like to like to write a procedure to insert this document into a parent Character Table, and a child CharacterFriend table, maintaining FK references.
If the ID was not provided it would be fairly easy to figure out what it was using SCOPE IDENTITY, and then filling that in before you inserted into the CharacterFriend Table.
However, it is less clear to me how you would discover the PK of the Character table in the event that there are multiple characters defined in the json document.
What would be the best way to write an insert procedure that establishes this FK in this case. The only thing that's jumping out at me is a cursor, but this feels wrong.
json sql-server sql-server-2016
Can you please post an example of json document with multiple characters defined?
– Andrea
Nov 11 at 14:25
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This link json part 3 open json shows how to shred json into parent child records.
Specifically showing how to generate multiple result sets from one Json Document
[
"CharacterID" : null,
"CharacterName" : "Egwene Al''Vere",
"IsFemale" : true,
"Address" :
"AddressLine1" : "Emonds Field",
"AddressLine2" : "The Two Rivers"
,
"Friends" : ["Rand Al''Thor", "Perrin Aybara", "Matrim Cauthon"],
"Children" : null
,
"CharacterID" : null,
"CharacterName" : "Second Character",
"IsFemale" : false,
"Address" :
"AddressLine1" : "Emonds Field 2",
"AddressLine2" : "The Three Rivers"
,
"Friends" : ["Jeff", "Zeke", "Sid Cauthon"],
"Children" : null
]
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
JSON_VALUE(@json, '$.CharacterName') AS CharacterName,
JSON_VALUE(@json, '$.IsFemale') AS IsFemale,
JSON_VALUE(@json, '$.Address.AddressLine1') AS AddressLine1,
JSON_VALUE(@json, '$.Address.AddressLine2') AS AddressLine2,
JSON_VALUE(@json, '$.Children') AS Children
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
OJ.value AS Friend
FROM OPENJSON(@json, '$.Friends') OJ;
I would like to like to write a procedure to insert this document into a parent Character Table, and a child CharacterFriend table, maintaining FK references.
If the ID was not provided it would be fairly easy to figure out what it was using SCOPE IDENTITY, and then filling that in before you inserted into the CharacterFriend Table.
However, it is less clear to me how you would discover the PK of the Character table in the event that there are multiple characters defined in the json document.
What would be the best way to write an insert procedure that establishes this FK in this case. The only thing that's jumping out at me is a cursor, but this feels wrong.
json sql-server sql-server-2016
This link json part 3 open json shows how to shred json into parent child records.
Specifically showing how to generate multiple result sets from one Json Document
[
"CharacterID" : null,
"CharacterName" : "Egwene Al''Vere",
"IsFemale" : true,
"Address" :
"AddressLine1" : "Emonds Field",
"AddressLine2" : "The Two Rivers"
,
"Friends" : ["Rand Al''Thor", "Perrin Aybara", "Matrim Cauthon"],
"Children" : null
,
"CharacterID" : null,
"CharacterName" : "Second Character",
"IsFemale" : false,
"Address" :
"AddressLine1" : "Emonds Field 2",
"AddressLine2" : "The Three Rivers"
,
"Friends" : ["Jeff", "Zeke", "Sid Cauthon"],
"Children" : null
]
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
JSON_VALUE(@json, '$.CharacterName') AS CharacterName,
JSON_VALUE(@json, '$.IsFemale') AS IsFemale,
JSON_VALUE(@json, '$.Address.AddressLine1') AS AddressLine1,
JSON_VALUE(@json, '$.Address.AddressLine2') AS AddressLine2,
JSON_VALUE(@json, '$.Children') AS Children
SELECT
JSON_VALUE(@json, '$.CharacterID') AS CharacterID,
OJ.value AS Friend
FROM OPENJSON(@json, '$.Friends') OJ;
I would like to like to write a procedure to insert this document into a parent Character Table, and a child CharacterFriend table, maintaining FK references.
If the ID was not provided it would be fairly easy to figure out what it was using SCOPE IDENTITY, and then filling that in before you inserted into the CharacterFriend Table.
However, it is less clear to me how you would discover the PK of the Character table in the event that there are multiple characters defined in the json document.
What would be the best way to write an insert procedure that establishes this FK in this case. The only thing that's jumping out at me is a cursor, but this feels wrong.
json sql-server sql-server-2016
json sql-server sql-server-2016
edited Nov 14 at 14:34
asked Nov 8 at 17:03
priehl
3462419
3462419
Can you please post an example of json document with multiple characters defined?
– Andrea
Nov 11 at 14:25
add a comment |
Can you please post an example of json document with multiple characters defined?
– Andrea
Nov 11 at 14:25
Can you please post an example of json document with multiple characters defined?
– Andrea
Nov 11 at 14:25
Can you please post an example of json document with multiple characters defined?
– Andrea
Nov 11 at 14:25
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212727%2fparse-and-store-parent-child-json-across-multiple-tables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you please post an example of json document with multiple characters defined?
– Andrea
Nov 11 at 14:25