Skip to content

Instantly share code, notes, and snippets.

@tullo
Forked from ipbastola/jq to filter by value.md
Last active January 29, 2021 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tullo/79363941f1e78ce404c023af6faffbb4 to your computer and use it in GitHub Desktop.
Save tullo/79363941f1e78ce404c023af6faffbb4 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))'
cat my.json | jq -c '.[] | select( ._id == 611 )
cat my.json | jq -c 'select( ._id == 611 )

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

@tullo
Copy link
Author

tullo commented Jan 29, 2021

You can also use or and and within select.

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

@tullo
Copy link
Author

tullo commented Jan 29, 2021

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 ) 

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