JQ: Parsing API Responses and Logs๐
Part of a pathway: Debugging With Nothing But a Terminal
Consult the map
-
Debugging With Nothing But a Terminal โ step 9 of 20
โ How Parsers Work ยท you are here ยท Working with YAML โ
It's 2am. The API is returning errors. You SSH into the server, curl the endpoint, and get back 500 lines of JSON. You squint at the terminal trying to find the error message buried somewhere in that wall of text. This is why jq exists.
jq is a lightweight and flexible command-line JSON processor. It's like sed, awk, and grep specifically designed for JSON data. For SREs and Platform Engineers, jq is an essential tool for parsing API responses, filtering logs, and transforming data during incident response and automation.
Installation๐
Before you can use jq, you need to install it:
Verify installation:
Quick Start: Get Productive in 5 Minutes๐
You can start using jq immediately with these essential patterns.

How JQ Works๐
jq operates on a stream of JSON entities. It takes an input, applies a filter, and sends the result to standard output.
graph TD
Input[JSON Input<br/>from API/file/pipe]
Filter["JQ Filter<br/>e.g., .items[]"]
Output[Transformed<br/>JSON/Text]
Input --> Filter
Filter --> Output
style Input fill:#2d3748,stroke:#cbd5e0,stroke-width:2px,color:#fff
style Filter fill:#2f855a,stroke:#cbd5e0,stroke-width:2px,color:#fff
style Output fill:#2d3748,stroke:#cbd5e0,stroke-width:2px,color:#fff
Three filters cover most of what you'll type in your first week with jq:
-
The Identity Filter (
.)
Why it matters: The simplest filter. It takes the input and outputs it exactly as is, but pretty-printed by default.
Pretty Print Key insight: Use this as your "first pass" to understand the structure of unknown JSON.
-
Object Identifier (
.foo)
Why it matters: Extracts the value associated with a key.
Extract Field Key insight: You can chain these for nested data:
.metadata.name. -
Array Iterator (
.[])
Why it matters: "Unpacks" an array, outputting each element individually.
Iterate Array Key insight: Essential for processing lists of pods, nodes, or log entries.
Why JQ Matters for Platform Work๐
In a world where everything is an API, JSON is the universal language. Whether you're debugging Kubernetes manifests, parsing CloudTrail logs, or interacting with a proprietary internal service, jq lets you cut through the noise.
Common Scenarios๐
Extract all pod names and their statuses from a namespace:
| List Pod Statuses | |
|---|---|
Find the Instance ID of all running EC2 instances with a specific tag:
| Filter AWS Instances | |
|---|---|
Parse structured JSON logs to find high-latency requests:
| Find Slow Requests | |
|---|---|
Common Pitfalls๐
Even experienced users hit these jq gotchas:
-
Forgetting to Quote the Filter
Your shell tries to expand
[]and{}as glob patterns beforejqever sees them. Quote the filter and the shell leaves it alone:Wrong - Shell Interprets Braces Correct - Always Quote -
Pipe Confusion
jquses|for its own pipeline. Don't confuse it with shell pipes:JQ Pipe (Inside Filter) Shell Pipe (Between Commands) -
Array vs Array Elements
.itemsreturns the whole array..items[]iterates elements:
Practice Problems๐
Practice Problem 1: Extracting from Arrays
Given the JSON {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}, how would you extract just the names of all users?
Answer
| Extract Names from an Array of Objects | |
|---|---|
.users part gets the array, [] iterates over its elements, and .name extracts the field from each element.
Practice Problem 2: Filtering
How would you filter a list of numbers [10, 25, 5, 40] to only show those greater than 20?
Answer
| Filter Numbers Greater Than 20 | |
|---|---|
select() is a powerful built-in function that keeps only the elements for which the expression inside is true.
Key Takeaways๐
| Filter | Description |
|---|---|
. |
The identity filter (pretty-prints input) |
.foo |
Extract field "foo" from an object |
.[] |
Iterate over elements in an array |
select(condition) |
Keep only elements matching the condition |
| |
Pipe the output of one filter into the next |
What's Next๐
If you're following the Debugging With Nothing But a Terminal pathway, the next step is Working with YAML on the Python site โ the same JSON/YAML data model you just filtered, from the Python side. If you'd rather stay on the command line, jump straight to yq: Wrangling YAML below in Essentials โ its syntax deliberately mirrors what you just learned here.
Further Reading๐
Official Documentation๐
- JQ Manual - The definitive reference for all filters and functions
- JQ Playground - Interactive online tool to test your
jqfilters - JQ Download - Installation packages for all platforms
Related Tools & Alternatives๐
- yq - Like
jqbut for YAML - fx - Terminal JSON viewer and processor with interactive UI
- jless - Modern JSON viewer with vim-style navigation
Deep Dives๐
- JQ Cookbook - Common patterns and recipes for complex transformations
- How Parsers Work - The lexing-then-parsing pipeline underneath every
jqcall, and why a malformed manifest fails the same way everywhere