Jeff Bailey
Written by Jeff Bailey

Learning resources, opinions, and facts about technology.

Learn jq

Reading Time: 2.0 minutes
Listen to this post

The Basics

The jq command-line JSON parser is a welcome tool in any programmer's toolbelt. Use jq to parse JSON to simplify collecting essential information from JSON files.

Primary Use Cases

  • Parse JSON
  • Query JSON
  • Reformat parsed JSON

Invalid Use Cases

  • Parsing raw text and XML

Parse A Remote File

Install curl and install jq.

Run this command to query a specific office name from offices.json.

curl --silent https://raw.githubusercontent.com/jeffabailey/learn/main/tools/jq/offices.json | jq .[].name | tr -d '"'

Press the green run button to see the output of this command.

Parse A Local File

Download offices.json and run this command in your download folder.

cat ./offices.json | jq .[].name

Press the green run button to see the output of this command.

Reformat A Local File

I am using the previously downloaded offices.json to run this command.

cat ./offices.json | jq '.[] | {name,location} | join(" is in ")'

Press the green run button to see the output of this command.

The join() function allows for quick formatting.

Try this command to get more flexible formatting.

cat ./offices.json | jq '.[] | .name + " is in " + .location'

Learn jq - Beyond the Basics