Managing JSON with jq

Update: I’ve released a workshopper-style interactive tutorial covering the basics of jq. Just getting started? Check it out!

For the longest time, I assumed that the way to debug API JSON was to pipe it through a pretty printer like python’s json.tool. Not the most concise solution, but it cleans up nicely enough:

$ curl | python -mjson.tool

    "email": "chico@ma.rx", 
    "id": 41, 
    "role": "user"
    "email": "harpo@ma.rx", 
    "id": 42, 
    "role": "user"
    "email": "groucho@ma.rx", 
    "id": 43, 
    "role": "admin"

But oh! How wrong I’ve been.

If it isn’t the greatest thing since sliced bread (at least for debugging JSON APIs), Stephen Dolan‘s jq has to be pretty close. Pretty print? Got it.

$ curl | jq .

Selectors? Done.

$ curl | jq '.[0] | { id, email }'

Filter, map, reduce? Check, check, check.

$ curl | jq '.[] | select(.id == 42)'
$ curl | jq 'map({ id, email })'
$ curl | jq 'reduce .[] as $item (0; . + $'

The list goes on and on.

Hey, I'm RJ! For more learnings about software and management, find me @rjzaworski or sign up for my semi-regular newsletter.

Let’s keep in touch

Send me timely updates on software, product, and process.