I was wondering how to go about breaking down a JSON array that's stored in another JSON, and how to best go about storing it. The JSON is
{
"InspectionID":1,
"RoomName":"Test1",
"Sections":[
{
"Name":"Floor",
"Notes":"Trash"
},
{
"Name":"Floor2",
"Notes":"Trash2"
}
]
}
Simplest thing in my mind is to just store as a varchar type in my table but doesn't seem to work because it's an array. Also read that its best to break any arrays into different tables with, which i'm fine with also because I can just reference it with RoomID in a new table. But how about do I go about actually breaking down this array and then inserting all the rows in one go? I'm using this currently to break down my JSON array in my SQL Server SP.
CREATE PROCEDURE [API].[AddRoom] (@UserID int, @JsonIn NVARCHAR(MAX), @JsonOut NVARCHAR (MAX) OUTPUT)
AS
BEGIN
Declare @InspectionID Int = JSON_VALUE(@JsonIn, '$.InspectionID')
Declare @RoomName varchar(200) = JSON_VALUE(@JsonIn, '$.RoomName')
Declare @Sections varchar(max) = JSON_VALUE(@JsonIn, '$.Sections')
INSERT INTO Rooms (InspectionID, RoomName, Sections)
SELECT @InspectionID, @RoomName, @Sections
set @JsonOut = (SELECT 1 [Status], 'Added new room' [Message] FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
END
Any info or tips are appreciated!