根据键值对从 JSON 文件中删除整个对象

Removing entire object from JSON file based on key value pair

我一直在努力从 Json 文件中删除抵消对象。我尝试使用 jq json 解析器方法,但没有任何结果。有人可以帮忙吗。

我要查找的是 – 无论文件中存在以下键和值对,都应删除整个对象。

{"name": "exception"}

输入:

{
  "results": [
    {
      "id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
      "retailerId": "1",
      "category": "exception",
      "context": {
        "sourceEvents": [
          "902bd449-881e-11eb-b603-29eb6c297e7d"
        ],
        "entityType": "ORDER"
      },
      "eventStatus": "FAILED",
      "attributes": [
        {
          "name": "exception",
          "value": {
            "code": 400,
            "message": "Failed to execute http call",
            "stackTrace": [
              {
                "fileName": "ReadOnlyFluentApiClient.java",
                "className": "com.fluentretail.api.v2.client.ReadOnlyFluentApiClient"
              }
            ],
            "suppressed": [],
            "suppressedExceptions": []
          },
          "type": "OBJECT"
        },
        {
          "name": "lastRule",
          "value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
          "type": "String"
        },
        {
          "name": "lastRuleSet",
          "value": "FindAndCreateDigitalFulfilment",
          "type": "String"
        },
        {
          "name": "message",
          "value": "Failed to execute http call",
          "type": "String"
        }
      ],
      "source": null,
      "generatedBy": "Rubix User",
      "generatedOn": "2021-03-18T19:17:51.517+0000"
    },
    {
      "id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
      "retailerId": "1",
      "category": "exception",
      "context": {
        "sourceEvents": [
          "902bd449-881e-11eb-b603-29eb6c297e7d"
        ],
        "entityType": "ORDER"
      },
      "eventStatus": "FAILED",
      "attributes": [
        {
          "name": "exception",
          "value": {
            "code": 400,
            "message": "Failed to execute http call",
            "stackTrace": [
              {
                "fileName": "ReadOnlyFluentApiClient.java",
                "className": "com.fluentretail.api.v2.client.ReadOnlyFluentApiClient"
              }
            ],
            "suppressed": [],
            "suppressedExceptions": []
          },
          "type": "OBJECT"
        },
        {
          "name": "lastRule",
          "value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
          "type": "String"
        },
        {
          "name": "lastRuleSet",
          "value": "FindAndCreateDigitalFulfilment",
          "type": "String"
        },
        {
          "name": "message",
          "value": "Failed to execute http call",
          "type": "String"
        }
      ],
      "source": null,
      "generatedBy": "Rubix User",
      "generatedOn": "2021-03-18T19:17:51.517+0000"
    }
  ]
}

预期输出为 -

{
  "results": [
    {
      "id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
      "retailerId": "1",
      "category": "exception",
      "context": {
        "sourceEvents": [
          "902bd449-881e-11eb-b603-29eb6c297e7d"
        ],
        "entityType": "ORDER"
      },
      "eventStatus": "FAILED",
      "attributes": [
        {
          "name": "lastRule",
          "value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
          "type": "String"
        },
        {
          "name": "lastRuleSet",
          "value": "FindAndCreateDigitalFulfilment",
          "type": "String"
        },
        {
          "name": "message",
          "value": "Failed to execute http call",
          "type": "String"
        }
      ],
      "source": null,
      "generatedBy": "Rubix User",
      "generatedOn": "2021-03-18T19:17:51.517+0000"
    },
    {
      "id": "a21f5193-881e-11eb-a0c1-3726f4a71fa9",
      "retailerId": "1",
      "category": "exception",
      "context": {
        "sourceEvents": [
          "902bd449-881e-11eb-b603-29eb6c297e7d"
        ],
        "entityType": "ORDER"
      },
      "eventStatus": "FAILED",
      "attributes": [
        {
          "name": "lastRule",
          "value": "ETOSUAT.base.ProposedFulfilmentWithoutInventory",
          "type": "String"
        },
        {
          "name": "lastRuleSet",
          "value": "FindAndCreateDigitalFulfilment",
          "type": "String"
        },
        {
          "name": "message",
          "value": "Failed to execute http call",
          "type": "String"
        }
      ],
      "source": null,
      "generatedBy": "Rubix User",
      "generatedOn": "2021-03-18T19:17:51.517+0000"
    }
  ]
}
walk(if type=="object" and .name == "exception"
     then empty else . end)

等价于:

walk(select(type=="object" and .name == "exception" | not))
del(..|select(type=="object" and .name=="exception"))

https://jqplay.org/s/il12Ribpdb

试试