Skip to content

Instantly share code, notes, and snippets.

@manisnesan
Forked from ipbastola/jq to filter by value.md
Last active March 4, 2024 13:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manisnesan/77c1508f52857b0fc11e8702e6eaa9d1 to your computer and use it in GitHub Desktop.
Save manisnesan/77c1508f52857b0fc11e8702e6eaa9d1 to your computer and use it in GitHub Desktop.
JQ Tricks

JQ to filter 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)

jq -c '. | select(.action == "problem_statement") | select(.context[] | contains("ipsum") | not) | .context[] | select(split(" ") | length < 20)' session_july_last_few_days.jsonl

Example

jq '. | select( .label == "USAGE")' annotated_data.jsonl
jq '. | select( .label == "TROUBLESHOOT")' annotated_data.jsonl
jq -c '. | select( .label == "")' annotated_data.jsonl

JQ to escape the dot

curl -s '$HOST/rs/search?q=poodle&rows=0' | jq '.responseHeader.params."lw.pipelineId"'

Credits

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