jq
MASTERY

// Query. Transform. Extract.

JSON IS EVERYWHERE.

APIs return JSON. Config files are JSON. NoSQL databases speak JSON. jq is your power tool for querying and transforming JSON data from the command line.

JSON AT SPEED.

Why write a Python script when jq can extract exactly what you need in one line? Filter complex JSON structures, reshape data, and build pipelines—all at terminal speed.

BEGIN YOUR JOURNEY

// Your Training Path

Click a lesson to begin

LESSON 01

Introduction to jq

What is jq? Installation and basics.

Beginner
LESSON 02

Basic Filters

Identity, object, and array access.

Beginner
LESSON 03

Object Access

Keys, values, and dot notation.

Beginner
LESSON 04

Array Access

Indices, slices, and iteration.

Beginner
LESSON 05

Pipe Operator

Chain jq filters together.

Beginner
LESSON 06

Constructors

Create arrays and objects.

Intermediate
LESSON 07

Conditionals

if-then-else and comparisons.

Intermediate
LESSON 08

Functions

map, select, flatten, and more.

Intermediate
LESSON 09

Working with Strings

String operations and interpolation.

Intermediate
LESSON 10

jq with APIs

curl, jq, and REST APIs.

Advanced
LESSON 11

Variables

Binding and using variables.

Advanced
LESSON 12

jq in Scripts

Automate jq in bash scripts.

Advanced

// Lesson 01: Introduction to jq

×

What is jq?

jq is a lightweight command-line JSON processor. It lets you filter, transform, and extract data from JSON with a powerful, expressive syntax.

Installation

# Debian/Ubuntu
sudo apt install jq

# macOS
brew install jq

# Check version
jq --version

Basic Syntax

jq 'FILTER' [FILE...]
echo 'JSON' | jq 'FILTER'

Simple Examples

# Pretty print JSON
echo '{"name":"Alice","age":30}' | jq '.'

# Identity filter (.)
cat data.json | jq '.'

# Validate JSON
cat data.json | jq . > /dev/null && echo "Valid JSON"

Quiz

1. What does jq do?

Show Answers
  1. Processes and filters JSON data

// Lesson 02: Basic Filters

×

Identity Filter (.)

The dot (.) filter returns the entire input unchanged. It's the most basic jq filter.

Examples

# Identity - output same as input
echo '{"a":1,"b":2}' | jq '.'

# Array
echo '[1,2,3]' | jq '.'

# Nested
echo '{"user":{"name":"Bob"}}' | jq '.'

Quiz

1. What does the identity filter (.) return?

Show Answers
  1. The entire input unchanged

// Lesson 03: Object Access

×

Dot Notation

Access object properties with dot notation: `.key`

Examples

# Access property
echo '{"name":"Alice","age":30}' | jq '.name'

# Nested access
echo '{"user":{"name":"Bob","email":"bob@example.com"}}' | jq '.user.email'

# Multiple properties
echo '{"name":"Alice","age":30}' | jq '.name, .age'

# Optional: ? for missing keys
echo '{"name":"Alice"}' | jq '.age?'

Bracket Notation

# Bracket notation (useful for special chars)
echo '{"user-name":"Alice"}' | jq '.["user-name"]'

# Keys with spaces
echo '{"first name":"Alice"}' | jq '.["first name"]'

Quiz

1. How do you access a nested property in jq?

Show Answers
  1. .parent.child (e.g., jq '.user.name')

// Lesson 04: Array Access

×

Array Indices

# First element
echo '[10,20,30]' | jq '.[0]'

# Last element
echo '[10,20,30]' | jq '.[-1]'

# Second element
echo '[10,20,30]' | jq '.[1]'

# Array length
echo '[10,20,30]' | jq 'length'

Slicing

# First two elements
echo '[10,20,30,40]' | jq '.[:2]'

# Elements 1-3 (indices 1, 2, 3)
echo '[10,20,30,40]' | jq '.[1:4]'

# Last two elements
echo '[10,20,30,40]' | jq '.[-2:]'

Quiz

1. How do you access the last element of an array?

Show Answers
  1. .[-1]

// Lesson 05: Pipe Operator

×

Chaining Filters

The pipe (|) passes output of one filter as input to the next, just like shell pipes.

Examples

# Get array, then first element
echo '{"items":[10,20,30]}' | jq '.items | .[0]'

# Shorthand
echo '{"items":[10,20,30]}' | jq '.items[0]'

# Chain multiple
echo '{"user":{"name":"Alice","posts":[{"title":"Hello"}]}}' | jq '.user.posts[0].title'

# Get all titles
echo '{"posts":[{"title":"A"},{"title":"B"}]}' | jq '.posts[].title'

Recursion Operator

# Recursively descend
echo '{"a":{"b":{"c":1}}}' | jq '..'

# Find all objects with key
echo '{"name":"Alice"}' | jq '.. | objects'

Quiz

1. What does the pipe operator (|) do in jq?

Show Answers
  1. Chains filters together, passing output to next filter

// Lesson 06: Constructors

×

Create Objects

# Object construction
echo '{}' | jq '{name: "Alice", age: 30}'

# From input
echo '{"first":"John","last":"Doe"}' | jq '{fullname: .first, last: .last}'

# With computed values
echo '{"name":"Alice","age":30}' | jq '{name: .name, adult: .age >= 18}'

Create Arrays

# Array construction
echo '[]' | jq '[.a, .b, .c]'

# From array
echo '[1,2,3]' | jq '[.[] | . * 2]'

# Collecting
echo '{"items":[1,2,3]}' | jq '[.items[] | select(. > 1)]'

Quiz

1. How do you create an object with computed values?

Show Answers
  1. {key: expression} e.g., jq '{name: .name, adult: .age >= 18}'

// Lesson 07: Conditionals

×

if-then-else

# Basic if
echo '{"age":25}' | jq 'if .age >= 18 then "adult" else "minor" end'

# Multiple conditions
echo '{"score":85}' | jq 'if .score >= 90 then "A" elif .score >= 80 then "B" else "C" end'

Comparisons

==    Equal
!=    Not equal
< >   Less, greater
<= >=  Less or equal, greater or equal
and   Boolean AND
or     Boolean OR
not   Boolean NOT

Quiz

1. What is the jq if-then-else syntax?

Show Answers
  1. if CONDITION then VALUE else VALUE end

// Lesson 08: Functions

×

Common Functions

map(FILTER)     Apply filter to each element
select(COND)    Keep elements matching condition
flatten         Flatten nested arrays
unique          Remove duplicates
sort            Sort arrays
keys            Get object keys
values          Get object values
type            Get value type

Examples

# Double each element
echo '[1,2,3]' | jq 'map(. * 2)'

# Select adults
echo '[{"age":10},{"age":25},{"age":17}]' | jq 'map(select(.age >= 18))'

# Get keys
echo '{"a":1,"b":2}' | jq 'keys'

# Unique and sort
echo '[3,1,2,3,1]' | jq 'unique | sort'

Quiz

1. What function filters array elements?

Show Answers
  1. select()

// Lesson 09: Working with Strings

×

String Functions

+        Concatenate strings
*        Repeat string
split(S)  Split into array
join(S)   Join array with separator
ascii_downcase  lowercase
ascii_upcase    UPPERCASE
ltrimstr(S)   Remove prefix
rtrimstr(S)   Remove suffix

Examples

# Concatenate
echo '"Hello"' | jq '. + " World"'

# Uppercase
echo '"hello"' | jq 'ascii_upcase'

# Split
echo '"a,b,c"' | jq 'split(",")'

# String interpolation with +
echo '{"name":"Alice"}' | jq '"Hello, " + .name'

Quiz

1. How do you convert a string to uppercase?

Show Answers
  1. jq 'ascii_upcase'

// Lesson 10: jq with APIs

×

curl + jq = Power

Query APIs and extract exactly what you need with curl piped to jq.

Examples

# GitHub API - get repo stars
curl -s api.github.com/repos/torvalds/linux | jq '.stargazers_count'

# Get all repo names
curl -s api.github.com/users/torvalds/repos | jq '.[].name'

# Get user's followers
curl -s api.github.com/users/torvalds | jq '{login, followers, public_repos}'

Practical Examples

# Weather API (example)
curl -s wttr.in/London.json | jq '{temp: .current_condition.temp_C, condition: .current_condition.weatherDesc[0].value}'

# Docker stats JSON
docker stats --no-stream --format '{{json .}}' | jq '{name: .Name, cpu: .CPUPerc, mem: .MemPerc}'

Quiz

1. What command fetches JSON from an API?

Show Answers
  1. curl -s (with -s for silent mode)

// Lesson 11: Variables

×

Variable Binding

# Assign to variable
echo '{"name":"Alice"}' | jq '.name as $name | "Hello \($name)"'

# Multiple variables
echo '{"first":"John","last":"Doe"}' | jq '.first as $f | .last as $l | "\($f) \($l)"'

let keyword (jq 1.7+)

# Define constant
echo '5' | jq 'let $x = 10; . + $x'

Quiz

1. How do you use a variable in jq string interpolation?

Show Answers
  1. "\($varname)"

// Lesson 12: jq in Scripts

×

Bash Script Examples

#!/bin/bash
# Fetch and parse API data
USERNAME="torvalds"
REPOS=$(curl -s "https://api.github.com/users/$USERNAME/repos" | jq -r '.[].name')

echo "Repos for $USERNAME:"
for repo in $REPOS; do
    stars=$(curl -s "https://api.github.com/repos/$USERNAME/$repo" | jq '.stargazers_count')
    echo "  $repo: $stars stars"
done

Process JSON Logs

#!/bin/bash
# Extract errors from JSON logs
jq -r 'select(.level=="error") | "\(.timestamp) \(.message)"' app.log.json > errors.txt

# Generate summary
jq '{errors: map(select(.level=="error")) | length, warnings: map(select(.level=="warning")) | length}' app.log.json

Congratulations!

You've mastered jq! You now understand:

  • Basic filters and object/array access
  • Pipe chaining
  • Object and array construction
  • Conditionals and comparisons
  • Built-in functions (map, select, keys)
  • String operations
  • API integration with curl
  • Variables and string interpolation

// Why jq

jq is essential for anyone working with APIs, configuration files, or any JSON data. It replaces complex Python or JavaScript scripts with elegant one-liners.

Master jq and you'll extract exactly what you need from any JSON data in seconds.

Query. Transform. Extract.

// Tools & References

jq Manual

Official documentation

jq manual

jq Playground

Try jq online

jqplay.org

jq Recipes

Common patterns

jq Recipes

jq Tutorial

Learning jq

Tutorial