skip to Main Content

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


  1. You need something like this :

    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,
           il.* -- this includes item, quantity ...
    FROM
        OPENJSON(@json, '$.document.Invoice.InvoiceLine') 
        WITH (
            _NavRecordId nvarchar(200) '$._NavRecordId',
            item nvarchar(100),
            Quantity int,
            PurchaseOrderLineNumber int
        ) AS il;
    

    Demo here

    Login or Signup to reply.
  2. Try this – SQL Server
    Just put the JSON to variable JSON1.
    DECLARE @json1 NVarChar(2048)

    SELECT
        JSON_VALUE(@json1, '$.document.Invoice._NavRecordId') AS NavRecordId,
        JSON_VALUE(@json1, '$.document.Invoice.InvoiceNumber') AS InvoiceNumber,
        JSON_VALUE(@json1, '$.document.Invoice.PurchaseOrderNumber') AS PurchaseOrderNumber,
        il._NavRecordId AS [_NavRecordId],
        il.item AS Item,
        il.Quantity,
        il.PurchaseOrderLineNumber
    FROM OPENJSON(@json1, '$.document.Invoice.InvoiceLine') 
    WITH (
        _NavRecordId nvarchar(100),
        item nvarchar(100) ,
        Quantity int ,
        PurchaseOrderLineNumber int 
    ) AS il;
    

    Result

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search