簡體   English   中英

SQL Server - 將 JSON 字符串解析為 SQL 表格式

[英]SQL Server - Parse JSON String to a SQL Table Format

我正在嘗試將 JSON 字符串轉換為 SQL Server 中的表格式。 我需要的元素和值實際上在“GetCustomReportResult”標簽中。 但是,當我嘗試執行以下查詢時收到一條錯誤消息:

消息 13609,級別 16,狀態 4,第 63 行 JSON 文本格式不正確。 在位置 916 處發現了意外字符“T”。

DECLARE @json NVARCHAR(MAX)
SET @json=
'{
   "status":"ok",
   "data":{
      "response":{
         "GetCustomReportResult":{
            "CIP":null,
            "CIQ":null,
            "Company":null,
            "ContractOverview":null,
            "ContractSummary":null,
            "Contracts":null,
            "CurrentRelations":null,
            "Dashboard":null,
            "Disputes":null,
            "DrivingLicense":null,
            "Individual":null,
            "Inquiries":{
               "InquiryList":null,
               "Summary":{
                  "NumberOfInquiriesLast12Months":0,
                  "NumberOfInquiriesLast1Month":0,
                  "NumberOfInquiriesLast24Months":0,
                  "NumberOfInquiriesLast3Months":0,
                  "NumberOfInquiriesLast6Months":0
               }
            },
            "Managers":null,
            "Parameters":{
               "Consent":True,
               "IDNumber":"124",
               "IDNumberType":"TaxNumber",
               "InquiryReason":"reditTerms",
               "InquiryReasonText":null,
               "ReportDate":"2021-10-04 06:27:51",
               "Sections":{
                  "string":[
                     "infoReport"
                  ]
               },
               "SubjectType":"Individual"
            },
            "PaymentIncidentList":null,
            "PolicyRulesCheck":null,
            "ReportInfo":{
               "Created":"2021-10-04 06:27:51",
               "ReferenceNumber":"60600749",
               "ReportStatus":"SubjectNotFound",
               "RequestedBy":"Jir",
               "Subscriber":"Credit",
               "Version":544
            },
            "Shareholders":null,
            "SubjectInfoHistory":null,
            "TaxRegistration":null,
            "Utilities":null
         }
      }
   },
   "errormsg":null
}'
SELECT *
FROM OPENJSON(@json);

JSON 字符串實際上存儲在 SQL 表中的一列中,如下所示,我正在嘗試以表格格式轉換標記“GetCustomReportResult”中的元素,其中包含每個“ApplicationID”的列和值。

在此處輸入圖片說明

這是我嘗試過但出現錯誤的方法:

消息 13609,級別 16,狀態 2,第 1 行 JSON 文本的格式不正確。 在位置 0 處發現了意外字符“o”。

SELECT 
y.cijreport,
y.ApplicationId,
JSON_VALUE(x.value, '$.CIP') as CIP,
JSON_VALUE(x.value, '$.CIQ') as CIQ
--other fields
FROM table as y
CROSS APPLY OPENJSON (cijreport) as x
where cijreport is not null

兩種選擇

如果 true 是文字,那么您缺少引號。

DECLARE @json NVARCHAR(MAX)
SET @json=
'{
   "status":"ok",
   "data":{
      "response":{
         "GetCustomReportResult":{
            "CIP":null,
            "CIQ":null,
            "Company":null,
            "ContractOverview":null,
            "ContractSummary":null,
            "Contracts":null,
            "CurrentRelations":null,
            "Dashboard":null,
            "Disputes":null,
            "DrivingLicense":null,
            "Individual":null,
            "Inquiries":{
               "InquiryList":null,
               "Summary":{
                  "NumberOfInquiriesLast12Months":0,
                  "NumberOfInquiriesLast1Month":0,
                  "NumberOfInquiriesLast24Months":0,
                  "NumberOfInquiriesLast3Months":0,
                  "NumberOfInquiriesLast6Months":0
               }
            },
            "Managers":null,
            "Parameters":{
               "Consent":"True",
               "IDNumber":"124",
               "IDNumberType":"TaxNumber",
               "InquiryReason":"reditTerms",
               "InquiryReasonText":null,
               "ReportDate":"2021-10-04 06:27:51",
               "Sections":{
                  "string":[
                     "infoReport"
                  ]
               },
               "SubjectType":"Individual"
            },
            "PaymentIncidentList":null,
            "PolicyRulesCheck":null,
            "ReportInfo":{
               "Created":"2021-10-04 06:27:51",
               "ReferenceNumber":"60600749",
               "ReportStatus":"SubjectNotFound",
               "RequestedBy":"Jir",
               "Subscriber":"Credit",
               "Version":544
            },
            "Shareholders":null,
            "SubjectInfoHistory":null,
            "TaxRegistration":null,
            "Utilities":null
         }
      }
   },
   "errormsg":null
}'
SELECT *
FROM OPENJSON(@json);

數據庫<>小提琴

如果true是布爾表達式,則刪除大寫的T並將其替換為t

DECLARE @json NVARCHAR(MAX)
SET @json=
'{
   "status":"ok",
   "data":{
      "response":{
         "GetCustomReportResult":{
            "CIP":null,
            "CIQ":null,
            "Company":null,
            "ContractOverview":null,
            "ContractSummary":null,
            "Contracts":null,
            "CurrentRelations":null,
            "Dashboard":null,
            "Disputes":null,
            "DrivingLicense":null,
            "Individual":null,
            "Inquiries":{
               "InquiryList":null,
               "Summary":{
                  "NumberOfInquiriesLast12Months":0,
                  "NumberOfInquiriesLast1Month":0,
                  "NumberOfInquiriesLast24Months":0,
                  "NumberOfInquiriesLast3Months":0,
                  "NumberOfInquiriesLast6Months":0
               }
            },
            "Managers":null,
            "Parameters":{
               "Consent":true,
               "IDNumber":"124",
               "IDNumberType":"TaxNumber",
               "InquiryReason":"reditTerms",
               "InquiryReasonText":null,
               "ReportDate":"2021-10-04 06:27:51",
               "Sections":{
                  "string":[
                     "infoReport"
                  ]
               },
               "SubjectType":"Individual"
            },
            "PaymentIncidentList":null,
            "PolicyRulesCheck":null,
            "ReportInfo":{
               "Created":"2021-10-04 06:27:51",
               "ReferenceNumber":"60600749",
               "ReportStatus":"SubjectNotFound",
               "RequestedBy":"Jir",
               "Subscriber":"Credit",
               "Version":544
            },
            "Shareholders":null,
            "SubjectInfoHistory":null,
            "TaxRegistration":null,
            "Utilities":null
         }
      }
   },
   "errormsg":null
}'
SELECT *
FROM OPENJSON(@json);

數據庫<>小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM