0

Given input like this:

{
  "type": "collection",
  "foo": "bar",
  "children": [
    {
      "properties": {
        "country": "GB"
      },
      "data": "..."
    },
    {
      "properties": {
        "country": "PL"
      },
      "data": "..."
    }
  ]
}

How can I use jq to retain all of the JSON structure, but filter out some of the children using select(). For instance, If I wanted to return only children with country GB, I would expect the following output:

{
  "type": "collection",
  "foo": "bar",
  "children": [
    {
      "properties": {
        "country": "GB"
      },
      "data": "..."
    }
  ]
}

If I only want the children, this is easy with .children[] | select(.properties.country == "GB"), but does not retain the rest of the JSON.

peak
  • 105,803
  • 17
  • 152
  • 177
cmbuckley
  • 40,217
  • 9
  • 77
  • 91

1 Answers1

2

The key is to use |=. In the present case, you could use the following pattern:

.children |= map(select(...))
peak
  • 105,803
  • 17
  • 152
  • 177