jq
jq
is a lightweight and flexible command-line tool used for processing JSON data. It allows users to parse, filter, transform, and manipulate JSON input, making it invaluable for working with structured data in shell scripts and command-line workflows.
Basic Syntax
Commonly Used Options
-r
: Output raw strings instead of JSON-encoded strings.-c
: Compact output, outputting JSON on a single line.-M
: Disables colored output (useful when piping to other tools).-f
: Specify a filter file instead of a command-line filter.
Examples
Basic Filter
Extract the value of a key from a JSON object:
Access Nested Values
Access a nested field in a JSON object:
Filter Arrays
Extract all items from an array:
Pipe Filters
Chain multiple filters together:
Modify Data
Update values in a JSON object:
Select Based on Condition
Filter JSON objects by a condition:
Use Variables
Pass shell variables into jq
filters:
Compact Output
Output JSON as a single line:
Raw Output
Get raw string output without quotes:
Conclusion
jq
is an essential tool for handling and processing JSON data efficiently from the command line. With its powerful filtering and transformation capabilities, it is indispensable for working with structured data in shell scripts and automation tasks.