【问题标题】:Update value in a JSON String更新 JSON 字符串中的值
【发布时间】:2021-12-10 07:36:20
【问题描述】:

这与我之前的帖子有关:Link to Previous Post

在 SQL 表的列中有这个 JSON 字符串:

看到完整的 JSON 字符串:

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);

我想更新 Consent 元素的值,在 value 周围加上引号“”,因为 JSON 很敏感并会导致错误。 Consent 元素位于 data.response.GetCustomReportResult.Parameters.Consent

然后我只想将更新后的 JSON 字符串列放入此代码中。大概可以用CTE或者子查询来实现?

    SELECT 
    y.cijreport,
    y.ApplicationId,
    x.CIP,
    x.CIQ
    --other fields
   FROM myTable as y
   CROSS APPLY OPENJSON (updated_cijreport, '$.data.response')
   WITH (
   CIP nvarchar(max) AS JSON,
   CIQ nvarchar(max) AS JSON
   ) AS x;

【问题讨论】:

    标签: sql json sql-server tsql common-table-expression


    【解决方案1】:

    您必须更新列中的值,这应该是原子的。使用 Json 作为值不是关系。

    无论如何,您可以像 Charlieface 所说的那样在字符串中进行替换,因为 json 只是 SQL Server 的 varchar。

    【讨论】:

      【解决方案2】:

      您可以使用嵌套的 REPLACE 函数(用于 TRUE 和 FALSE)来更新列,以便引用 VALUE 字符串。 UPDATE 后 ISJSON 函数返回 1 表示 JSON 有效。

      DECLARE @json       table(ApplicationId varchar(20),
                                cijreport     NVARCHAR(MAX));
      insert @json(ApplicationId, cijreport) values
      ('C3272473021100', N'{
      "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
       }');
      
      update @json
      set cijreport=replace(replace(cijreport, '"Consent":False', '"Consent":"False"'),
                                               '"Consent":True', '"Consent":"True"')
      where ApplicationId='C3272473021100';
      
      select isjson(cijreport) is_valid_json from @json;
      
      is_valid_json
      1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多