> For the complete documentation index, see [llms.txt](https://til.yulrizka.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://til.yulrizka.com/unix/filtering-json-with-jq.md).

# filtering json with jq

[jq](https://stedolan.github.io/jq/) is a fantastic command line tools to parse and filter json in command line. It works for linux, osx and maybe windows.

example, to format json

```
$ json='[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]'
$ echo $json
[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]

$ echo $json | jq .
[
  {
    "genre": "deep house"
  },
  {
    "genre": "progressive house"
  },
  {
    "genre": "dubstep"
  }
]
```

To output a particular field

```
$ echo $json | jq '.[].genre'
"deep house"
"progressive house"
"dubstep"
```

To filter based on a key

```
$ echo "$json" | jq -c '.[] | select(.genre | contains("house"))'
{"genre":"deep house"}
{"genre":"progressive house"}
```

more example could be found on [here](https://github.com/stedolan/jq/wiki/Cookbook)
