Home > other >  Filter nested JSON but maintain the object structure
Filter nested JSON but maintain the object structure

Time:08-05

Consider the below JSON:

[
 {
 "vd":[    
        {
            "key":1
        },
        {
            "key":2    
        }
      ]   
  }
]

Now I would like to filter based on the filter key==1. So the result should be:

[
 {
    "vd": [
        {
            "key": 1
        }
    ]
  }
 ]

Is there a way to achieve this using jq?

CodePudding user response:

Traverse to the array in question .[].vd, and update |= with a map based on a select using your criteria .key == 1:

jq '.[].vd |= map(select(.key == 1))' 
[
  {
    "vd": [
      {
        "key": 1
      }
    ]
  }
]

Demo

  • Related