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

Remove elements from a JSON array, with no key

我正在寻找一种可以从 SQL 服务器中的 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
      )
   )
)

错误:

Msg 13619, Level 16, State 1, Line 4
Unsupported JSON path found in argument 2 of JSON_MODIFY.

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

[{"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 字符串的根元素。

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

您不必在此处使用 JSON_MODIFY。您可以 select 您想要的数据作为 table,过滤它,然后 re-encode 作为 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
    }
]