116

I'm using jq to play with json. I was wondering how to conditionally print something in that.

Say I am interested in a field call geo. I used the following command and find out there is only one entry whose geo is null:

% cat all.json | jq '.geo != null' | sort | uniq -c              
   1 false
6891 true

How can I print out that entry only without printing everything else?

Didn't see something like print command in the manual. And this doesn't work: cat all.json | jq 'if .place == null then . end'. jq complained about syntax error.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
clwen
  • 20,004
  • 31
  • 77
  • 94

1 Answers1

188

You can use the select function to get only required entries:

jq 'select(.geo != null)' all.json
max taldykin
  • 12,459
  • 5
  • 45
  • 64
  • 27
    The select works, but to fully answer the question you'll need to filter only the geo again, piping inside jq: `cat all.json | jq 'select(.geo != null) | .geo' > geo-only.json` – Andy Reagan Jun 13 '16 at 18:48
  • Just a reminder that the `cat` is unnecessary, you could do `jq 'select(.geo != null) | .geo' all.json > geo-only.json` – João Ciocca Jan 06 '23 at 20:30