Linux Command: jq
Command-line JSON processor
The jq command is a lightweight and flexible command-line JSON processor. It is like sed, awk, and grep for JSON data โ allowing you to slice, filter, map, and transform structured data with ease. jq is essential for modern system administration and DevOps work, where JSON is the standard format for API responses, configuration files, container metadata, and cloud service outputs. Whether you are parsing Kubernetes manifests, processing API responses from curl, or extracting values from Terraform state files, jq makes working with JSON on the command line practical and powerful. jq uses its own domain-specific language for writing filters and transformations. Despite its compact syntax, jq is surprisingly powerful โ supporting conditionals, regular expressions, string interpolation, user-defined functions, and even recursive descent. It reads JSON from stdin or files, applies your filter expression, and outputs the result as formatted JSON (or raw text with -r).
Syntax
jq [OPTIONS] FILTER [FILE...]Key Options
.โ Identity filter - output the entire input unchanged.keyโ Extract a specific key from an object-rโ Raw output - strip quotes from strings-cโ Compact output - no pretty printing-eโ Exit with error if output is null or false-sโ Slurp - read all inputs into an array
Examples
Extract a field from JSON
echo '{"name":"nginx","version":"1.25"}' | jq -r .nameOutput: nginx
Get nested values
echo '{"server":{"host":"10.0.0.1","port":8080}}' | jq '.server.port'Output: 8080
Filter array elements
echo '[{"name":"a","active":true},{"name":"b","active":false}]' | jq '.[] | select(.active==true) | .name'Output: "a"
Pro Tips
- When assigning jq output to shell variables, use -r to get raw strings without quotes: VERSION=$(jq -r .version package.json)
- jq returns "null" (string) for missing keys. Use the // operator for defaults: jq '.missing // "default"' to avoid null in your scripts.
- Use jq . file.json to validate and pretty-print JSON. If the file has syntax errors, jq will report the exact error location.
Learn more: Full jq reference โ