0

我一直在研究将 Xero Report 数据引入我们的数据仓库的问题。Xero 生成数据的方式很烦人,而且嵌套很深。

{
  "Id": "2af62c5f-dc79-4b6d-9658-bd94446e3c01",
  "Status": "OK",
  "ProviderName": "Data Warehouse",
  "DateTimeUTC": "/Date(1626326365968)/",
  "Reports": [
    {
      "ReportID": "ProfitAndLoss",
      "ReportName": "Profit and Loss",
      "ReportType": "ProfitAndLoss",
      "ReportTitles": [
        "Profit and Loss",
        "ACME",
        "1 July 2021 to 31 July 2021"
      ],
      "ReportDate": "15 July 2021",
      "UpdatedDateUTC": "/Date(1626326365968)/",
      "Fields": [],
      "Rows": [
        {
          "RowType": "Header",
          "Cells": [
            {
              "Value": ""
            },
            {
              "Value": "31 Jul 21"
            }
          ]
        },
        {
          "RowType": "Section",
          "Title": "Less Cost of Sales",
          "Rows": [
            {
              "RowType": "Row",
              "Cells": [
                {
                  "Value": "EPAY",
                  "Attributes": [
                    {
                      "Value": "10f71a2c-afa9-4005-824b-38c722896e44",
                      "Id": "account"
                    }
                  ]
                },
                {
                  "Value": "32556.90",
                  "Attributes": [
                    {
                      "Value": "10f71a2c-afa9-4005-824b-38c722896e44",
                      "Id": "account"
                    }
                  ]
                }
              ]
            },
            {
              "RowType": "Row",
              "Cells": [
                {
                  "Value": "Purchases",
                  "Attributes": [
                    {
                      "Value": "b4dd09f7-37e6-4ac5-87a0-61d11b5cd4c0",
                      "Id": "account"
                    }
                  ]
                },
                {
                  "Value": "1135980.98",
                  "Attributes": [
                    {
                      "Value": "b4dd09f7-37e6-4ac5-87a0-61d11b5cd4c0",
                      "Id": "account"
                    }
                  ]
                }
              ]
            }
        }
    }
}

此 JSON 是直接 REPORT API JSON 的示例。

到目前为止,我一直在使用 SQL 语句来摄取它,我有这个,

DECLARE @json NVARCHAR(1000)
SELECT @json = 
N'
<JSON here<'     
SELECT
        JSON_Value (c.value, '$.Reports.ReportID') as ReportID,
        JSON_Value (c.value, '$.Reports.ReportDate') as ReportDate,
        JSON_Value (c.value, '$.Reports.Report') as ReportTitle,
        JSON_Value (p.value, '$.Reports.Rows.RowType') as RowType, 
        JSON_Value (p.value, '$.Reports.Rows.RowType.Row.Cells.Value') as ReportValue
      
    FROM OPENJSON (@json, '$.Reports.ReportID') as c
    CROSS APPLY OPENJSON (c.value, '$.Reports.Rows.RowType') as p

我现在已经碰壁了,不知道从哪里开始。希望有一些建议可以帮助我进入下一步。

4

1 回答 1

0

有一个快速的小提琴,并得到了这个:

     Select  * FROM OPENJSON((@json))  WITH 
        (
            
            
            Reports NVARCHAR(MAX)'$.Reports' AS JSON
           

        ) AS JsonProccessingData
        CROSS APPLY OPENJSON (Reports) WITH
        (
            ReportID VARCHAR(500) '$.ReportID',
            ReportDate VARCHAR(200) '$.ReportDate',
            ReportTitle VARCHAR(200) '$.ReportName',
            Rows NVARCHAR(MAX)'$.Rows' AS JSON
        )
        CROSS APPLY OPENJSON (Rows) WITH
        (
            RowType VARCHAR(500) '$.RowType',
            Cells NVARCHAR(MAX)'$.Cells' AS JSON
        )
        CROSS APPLY OPENJSON (Cells) WITH
        (
            ReportValue NVARCHAR(MAX)'$.Value' AS JSON
        )

不知道如何处理多个值,希望这有点帮助?

于 2021-07-19T12:41:00.713 回答