繁体   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