Skip to content

Instantly share code, notes, and snippets.

@ipbastola
Last active May 22, 2026 18:53
Show Gist options
  • Select an option

  • Save ipbastola/2c955d8bf2e96f9b1077b15f995bdae3 to your computer and use it in GitHub Desktop.

Select an option

Save ipbastola/2c955d8bf2e96f9b1077b15f995bdae3 to your computer and use it in GitHub Desktop.
JQ to filter JSON by value

JQ to filter JSON by value

Syntax: cat <filename> | jq -c '.[] | select( .<key> | contains("<value>"))'

Example: To get json record having _id equal 611

cat my.json | jq -c '.[] | select( ._id | contains(611))'

Remember: if JSON value has no double quotes (eg. for numeric) to do not supply in filter i.e. in contains(611)

@jalkjaer

Copy link
Copy Markdown

if searching for exact value don't use contains as that is substring/array lookup )
use ==
i.e.

cat my.json | jq -c '.[] | select( ._id == 611 ) 

And if your input is not a large array, but rather individual lines of json then use

cat my.json | jq -c 'select( ._id == 611 ) 

@mikeschinkel

Copy link
Copy Markdown

Nice! Thanks.

@pmithil7

pmithil7 commented Oct 2, 2019

Copy link
Copy Markdown

This is helpful. Can we add two select statements and do a 'AND' or an 'OR' of those select statements?

@pmithil7

pmithil7 commented Oct 2, 2019

Copy link
Copy Markdown

I'm answering my own question. It is possible by piping one select statement into another for an AND operation. Not sure about the OR.

@shillbit

Copy link
Copy Markdown

you're a life saver... thanks

@aldnav

aldnav commented Jun 17, 2020

Copy link
Copy Markdown

I'm answering my own question. It is possible by piping one select statement into another for an AND operation. Not sure about the OR.

You can also use or and and within select.

cat my.json | jq -c '.[] | select( ._id == 611 or .author == "John Doe" )'

https://stedolan.github.io/jq/manual/#ConditionalsandComparisons

@smokinggoats

Copy link
Copy Markdown

Thanks a lot for this and for the answers ❤️

@ST-DDT

ST-DDT commented Oct 7, 2021

Copy link
Copy Markdown

Note: If you need the output to be an array, then you should use map:

cat my.json | jq -c 'map( select( ._id == 611 ) )'

@tripleee

Copy link
Copy Markdown

Tangentially, as ever, you want to avoid the useless cat.

Instead of

cat file | jq ...

you want

jq ... file

@samrjack

samrjack commented Mar 3, 2023

Copy link
Copy Markdown

@tripleee When using it in a script you are right, but when doing iterative testing on the command line, not having to to jump back through the file name every time is undeniably helpful.

@jcallen

jcallen commented Jun 22, 2023

Copy link
Copy Markdown

@samrjack Another way to do the same without changing the order on the command line is to replace:

cat file | jq ...

with:

< file jq ...

You can put the < file anywhere on the command line, not just at the end

@mikeschinkel

Copy link
Copy Markdown

@jcallen — Very cool!

And as they say, "I learn something new everyday!" 🙂

@gangsta

gangsta commented Oct 15, 2023

Copy link
Copy Markdown

If you're looking after - how to parse JSON and select date range
Look at here https://gist.github.com/gangsta/702e071fd048db2e39c7907f40d0cfd4

@mscomparin

Copy link
Copy Markdown

To get an array back and persist it to a file, I wrapped the whole expression in an array like this:

cat input.json | jq '[ .[] | select( .field | contains("something")) ]' > output.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment