user2144692
user2144692

Reputation: 3

How to filter some array in a sub-object with object in a json file with jq

I need to filter a JSON with a nested strucutre like below. All objects in array b where attribute x contains a "z" in the value of x should be filtered out. The rest should stay in the file.

{
    "a": {
        "b": [
            {
                "c": "1",
                "x": "aaa",
            },
            {
                "c": "2",
                "x": "aza",
            },
            {
                "c": "7",
                "x": "azb",
            }
        ]
    },
    "d": {
        "e": [
            "1"
        ],
        "f": [
            "2"
        ]
    }
}

Expected output:

{
  "a": {
    "b": [
      {
        "c": "1",
        "x": "aaa"
      }
    ]
  },
  "d": {
    "e": [
      "1"
    ],
    "f": [
      "2"
    ]
  }
}

Upvotes: 0

Views: 75

Answers (1)

oguz ismail
oguz ismail

Reputation: 50815

use select with contains:

jq '.a.b|=[.[]|select(.x|contains("z")|not)]' file

Upvotes: 1

Related Questions