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.










share|improve this question























  • Can you please post an example of json document with multiple characters defined?
    – Andrea
    Nov 11 at 14:25














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.










share|improve this question























  • Can you please post an example of json document with multiple characters defined?
    – Andrea
    Nov 11 at 14:25












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)