I am new to sqlserver. I want to convert json data into records and insert into a table. I have json data like
{
"HEADER":[
{
"seq_id":343,
"max_processed_scn":649044274,
"time_processed":"13-MAR-19 09.03.23.081055 PM",
"status":"COMPLETED"
},
{
"seq_id":344,
"max_processed_scn":649214903,
"time_processed":"15-MAR-19 12.49.31.606172 PM",
"status":"COMPLETED"
}
],
"DETAIL":[
{
"seq_id":11215,
"event_id":1,
"caller_seq":343,
"event_description":"BEGIN.."
},
{
"seq_id":11216,
"event_id":2,
"caller_seq":343,
"event_description":"STARTING"
},
{
"seq_id":11217,
"event_id":2,
"caller_seq":343,
"event_description":"STARTED "
} "event_description":"TOTAL"
]
}
so the array "header" I want to insert into one table and the other array "Detail" into another.Can anyone help me on achieving this. Thanks in advance
so I need detail table data like
seq_id max_processed_scn time_processed status
343 649044274 13-MAR-19 09.03.23.081055 PM COMPLETED
344 649214903 15-MAR-19 12.49.31.606172 PM COMPLETED
I used this query :
DECLARE @json NVARCHAR(MAX)
SET @json =
N'[{
"HEADER":[
{
"seq_id":343,
"max_processed_scn":649044274,
"time_processed":"13-MAR-19 09.03.23.081055 PM",
"status":"COMPLETED"
},
{
"seq_id":344,
"max_processed_scn":649214903,
"time_processed":"15-MAR-19 12.49.31.606172 PM",
"status":"COMPLETED"
}
],
"DETAIL":[
{
"seq_id":11215,
"event_id":1,
"caller_seq":343,
"event_description":"BEGIN.."
},
{
"seq_id":11216,
"event_id":2,
"caller_seq":343,
"event_description":"STARTING"
},
{
"seq_id":11217,
"event_id":2,
"caller_seq":343,
"event_description":"STARTED "
} "event_description":"TOTAL"
]
}
]'
SELECT *
FROM OPENJSON(@json, '$.HEADER')
WITH (seq_id INT, max_processed_scn INT, time_processed NVARCHAR(100), [status] NVARCHAR(100))
OPENJSON(e.g.SELECT * FROM OPENJSON(@json, '$.HEADER') WITH (seq_id INT, max_processed_scn INT, time_processed NVARCHAR(100), [status] NVARCHAR(100))and another one forDETAIL). If you don't, it's almost not worth doing in T-SQL; client code is much better equipped.HEADERSattribute. It's an object that containsHEADERandDETAILproperties. Those contain arrays. At the very least you need to fix the names and access the array elements