Search code examples
jsonjqkey-valuedel

Remove parent elements with certain key-value pairs using JQ


I need to remove elements from a json file based on certain key values. Here is the file I am trying to process.

{
  "element1": "Test Element 1",
  "element2": {
    "tags": "internal",
    "data": {
      "data1": "Test Data 1",
      "data2": "Test Data 2"
    }
  },
  "element3": {
    "function1": {
      "tags": [
        "new",
        "internal"
      ]
    },
    "data3": "Test Data 3",
    "data4": "Test Data 4"
  },
  "element4": {
    "function2": {
      "tags": "new"
    },
    "data5": "Test Data 5"
  }
}

I want to remove all elements that have a "tag" with value "internal". So the result should look like this:

{
  "element1": "Test Element 1",
  "element4": {
    "function2": {
      "tags": "new"
    },
    "data5": "Test Data 5"
  }
}

I tried various approaches but I just don't get it done using jq. Any ideas? Thanks.

Just to add some more complexity. Let's assume the json is:

{
  "element1": "Test Element 1",
  "element2": {
    "tags": "internal",
    "data": {
      "data1": "Test Data 1",
      "data2": "Test Data 2"
    }
  },
  "element3": {
    "function1": {
      "tags": [
        "new",
        "internal"
      ]
    },
    "data3": "Test Data 3",
    "data4": "Test Data 4"
  },
  "element4": {
    "function2": {
      "tags": "new"
    },
    "data5": "Test Data 5"
  },
  "structure1" : {
    "substructure1": {
      "element5": "Test Element 5",
      "element6": {
        "tags": "internal",
        "data6": "Test Data 6"
      }
    }
  }
}

and I want to get

{
  "element1": "Test Element 1",
  "element4": {
    "function2": {
      "tags": "new"
    },
    "data5": "Test Data 5"
  },
  "structure1" : {
    "substructure1": {
      "element5": "Test Element 5",
    }
  }
}

Solution

  • Not easy, finding elements which have a tags key somewhere whose value is either the string internal, or an array of which an element is the string internal in a reliable way is only possible with a complex boolean expression as below.

    Once found, deleting them can be done using the del built-in.

    del(.[] | first(select(recurse
      | objects
      | has("tags") and (.tags
        | . == "internal" or (
          type == "array" and index("internal")
        )
      )
    )))
    

    Online demo