从 JSON 数组中删除元素,没有键

Posted

技术标签:

【中文标题】从 JSON 数组中删除元素,没有键【英文标题】:Remove elements from a JSON array, with no key 【发布时间】:2020-11-08 19:25:46 【问题描述】:

我正在寻找一种可以从 SQL Server 中的 JSON 数组中删除元素的方法,我尝试使用 JSON_MODIFY/OPENPATH,但似乎无法正确获取路径参数。

这是我想要做的转换的示例:

["FieldName":"OrderStatusTypeId","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2,"FieldName":"PatientReferenceNumber","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2]

成为:

["FieldName":"OrderStatusTypeId","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2]

基本上,我想删除数组的任何元素,其中Action 为2,ValueBeforeValueAfter 字段相同。

这是我目前正在尝试的测试,但我不断收到以下错误:

DECLARE @JSONData AS NVARCHAR(4000)  
SET @JSONData = N'["FieldName":"OrderStatusTypeId","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2,"FieldName":"PatientReferenceNumber","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"PoNumber","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"ReferringPhysicianName","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"InsuranceProvider","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"TreatmentId","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"RowVersion[6]","FieldType":"Byte","ValueBefore":"10","ValueAfter":"115","Action":2,"FieldName":"OrderStatusType.Id","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2,"FieldName":"OrderStatusType.Description","FieldType":"String","ValueBefore":"Delivered","ValueAfter":"Failed Logistics","Action":2,"FieldName":"OrderStatusType.IsSelectable","FieldType":"Boolean","ValueBefore":"False","ValueAfter":"True","Action":2]'

SELECT @JSONData = JSON_MODIFY(@JSONData, '$',
   JSON_QUERY(
      (
      SELECT *
      FROM OPENJSON(@JSONData, '$') WITH (
         FieldName nvarchar(1000) '$.FieldName',
         FieldType nvarchar(1000) '$.FieldType',
         ValueBefore nvarchar(1000) '$.ValueBefore',
         ValueAfter nvarchar(1000) '$.ValueAfter',
         Action int '$.Action'
      )
      WHERE Action <> 2 AND ValueBefore <> ValueAfter
      FOR JSON PATH
      )
   )
)

错误:

消息 13619,第 16 层,状态 1,第 4 行 在 JSON_MODIFY 的参数 2 中发现不受支持的 JSON 路径。

我正在寻找的预期输出是:

["FieldName":"OrderStatusTypeId","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2,"FieldName":"RowVersion[6]","FieldType":"Byte","ValueBefore":"10","ValueAfter":"115","Action":2,"FieldName":"OrderStatusType.Id","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2,"FieldName":"OrderStatusType.Description","FieldType":"String","ValueBefore":"Delivered","ValueAfter":"Failed Logistics","Action":2,"FieldName":"OrderStatusType.IsSelectable","FieldType":"Boolean","ValueBefore":"False","ValueAfter":"True","Action":2]

如何计算出正确的 JSON 路径值,我似乎在网上找到的所有示例都没有数组作为 JSON 字符串的根元素。

注意,元素的顺序并不重要。

【问题讨论】:

【参考方案1】:

您不必在此处使用 JSON_MODIFY。您可以选择您想要的数据作为表格,对其进行过滤,然后将其重新编码为 JSON。

DECLARE @JSONData AS NVARCHAR(4000)  
SET @JSONData = N'["FieldName":"OrderStatusTypeId","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2,"FieldName":"PatientReferenceNumber","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"PoNumber","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"ReferringPhysicianName","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"InsuranceProvider","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"TreatmentId","FieldType":"String","ValueBefore":"","ValueAfter":"","Action":2,"FieldName":"RowVersion[6]","FieldType":"Byte","ValueBefore":"10","ValueAfter":"115","Action":2,"FieldName":"OrderStatusType.Id","FieldType":"Int32","ValueBefore":"8","ValueAfter":"10","Action":2,"FieldName":"OrderStatusType.Description","FieldType":"String","ValueBefore":"Delivered","ValueAfter":"Failed Logistics","Action":2,"FieldName":"OrderStatusType.IsSelectable","FieldType":"Boolean","ValueBefore":"False","ValueAfter":"True","Action":2]'

set @JSONData = 
(
    SELECT *
    FROM OPENJSON(@JSONData, '$') WITH (
        FieldName nvarchar(1000) '$.FieldName',
        FieldType nvarchar(1000) '$.FieldType',
        ValueBefore nvarchar(1000) '$.ValueBefore',
        ValueAfter nvarchar(1000) '$.ValueAfter',
        Action int '$.Action'
    )
    WHERE not (Action = 2 and ValueBefore = ValueAfter)
    FOR JSON PATH
)

JSON 是

[
    
        "FieldName": "OrderStatusTypeId",
        "FieldType": "Int32",
        "ValueBefore": "8",
        "ValueAfter": "10",
        "Action": 2
    ,
    
        "FieldName": "RowVersion[6]",
        "FieldType": "Byte",
        "ValueBefore": "10",
        "ValueAfter": "115",
        "Action": 2
    ,
    
        "FieldName": "OrderStatusType.Id",
        "FieldType": "Int32",
        "ValueBefore": "8",
        "ValueAfter": "10",
        "Action": 2
    ,
    
        "FieldName": "OrderStatusType.Description",
        "FieldType": "String",
        "ValueBefore": "Delivered",
        "ValueAfter": "Failed Logistics",
        "Action": 2
    ,
    
        "FieldName": "OrderStatusType.IsSelectable",
        "FieldType": "Boolean",
        "ValueBefore": "False",
        "ValueAfter": "True",
        "Action": 2
    
]

【讨论】:

以上是关于从 JSON 数组中删除元素,没有键的主要内容,如果未能解决你的问题,请参考以下文章

从数组中删除元素的 JSON 补丁格式是啥?

在mysql json中按值删除数组元素

如何从 Spring Boot 中的 JSON 数组中删除元素? [关闭]

将 JSON 元素放入数组中

如果它为空,我如何从我的 JSON 中删除一个密钥

从 JSON 数组拼接或删除元素列表