jsonjqglobaleditremoveall

Removing entire object from JSON file based on key value pair


I been struggling removing set-off objects from Json file. I tried with jq json parser method but nothing has worked out. Could someone please help on this.

What am looking for is – Wherever the below key and value pair are present in a file, the entire object should be removed.

{"name": "exception"}

Input:

{
  "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"
    }
  ]
}

Expected output is -

{
  "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"
    }
  ]
}

Solution

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

    Try it at https://jqplay.org/s/il12Ribpdb