I’m trying to extract the following JSON message using SQL:
{
"document": {
"Invoice": {
"_NavRecordId": "Purch. Inv. Header: 06IF+230033",
"InvoiceNumber": "06IF+230033",
"PurchaseOrderNumber": "P0032259",
"InvoiceLine": [
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,1",
"item": "A05284",
"Quantity": "2",
"PurchaseOrderLineNumber": "1"
},
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,2",
"item": "A00019",
"Quantity": "10",
"PurchaseOrderLineNumber": "2"
}
]
}
}
}
Unfortunately I’m not able to get this data to a table with every InvoiceLine as it’s own record.
I tried the following code already:
DECLARE @json NVARCHAR(MAX) = '{
"document": {
"Invoice": {
"_NavRecordId": "Purch. Inv. Header: 06IF+230033",
"InvoiceNumber": "06IF+230033",
"PurchaseOrderNumber": "P0032259",
"InvoiceLine": [
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,1",
"item": "A05284",
"Quantity": "2",
"PurchaseOrderLineNumber": "1"
},
{
"_NavRecordId": "Purch. Inv. Line: 06IF+230033,2",
"item": "A00019",
"Quantity": "10",
"PurchaseOrderLineNumber": "2"
}
]
}
}
}';
SELECT
JSON_VALUE(@json, '$.document.Invoice._NavRecordId') AS NavRecordId,
JSON_VALUE(@json, '$.document.Invoice.InvoiceNumber') AS InvoiceNumber,
JSON_VALUE(@json, '$.document.Invoice.PurchaseOrderNumber') AS PurchaseOrderNumber,
JSON_VALUE(@json, '$.document.Invoice.InvoiceLine.item') AS Item,
JSON_VALUE(@json, '$.document.Invoice.InvoiceLine.Quantity') AS Quantity,
JSON_VALUE(@json, '$.document.Invoice.InvoiceLine.PurchaseOrderLineNumber') AS PurchaseOrderLineNumber
FROM
OPENJSON(@json, '$.document.Invoice.InvoiceLine')
WITH (
_NavRecordId nvarchar(100) '$._NavRecordId',
item nvarchar(100),
Quantity int,
PurchaseOrderLineNumber int
) AS il;
Unfortunately I’m not able to fill the item, quantity and the PurchaseOrderLineNumber with the respected data
2
Answers
You need something like this :
Demo here
Try this – SQL Server
Just put the JSON to variable JSON1.
DECLARE @json1 NVarChar(2048)
Result