Skip to content

jq

Commonly used jq commands for parsing and transforming JSON data.

Basic Filters

Essential commands to extract and manipulate JSON values.

Extract a key's value

Selects the value of a specific key in a JSON object.

Terminal window
jq '.key'

Pretty-print JSON

Formats JSON with indentation for better readability.

Terminal window
jq '.'

Filtering and Transforming

Methods to search, modify, and transform JSON data.

Select objects with a condition

Filters objects based on a condition.

Terminal window
jq 'select(.age > 18)'

Map over an array

Applies a function to each element in an array.

Terminal window
jq 'map(.key)'

Flatten nested arrays

Flattens an array of arrays into a single array.

Terminal window
jq 'flatten'

Extract specific fields from objects in an array

Creates an array of selected fields from objects.

Terminal window
jq 'map({name: .name, age: .age})'

Advanced Techniques

Powerful transformations using jq’s built-in functions.

Construct new JSON objects

Builds a new object with selected properties.

Terminal window
jq '{name: .name, age: .age}'

String interpolation

Formats strings dynamically using variables.

Terminal window
jq '"Hello \(.name)!"'

Group by a key

Groups objects in an array by a specific key.

Terminal window
jq 'group_by(.category)'

Count occurrences of values

Counts occurrences of unique values in an array.

Terminal window
jq 'group_by(.) | map({key: .[0], count: length})'

Convert JSON to CSV

Converts an array of objects into CSV format.

Terminal window
jq -r 'map([.name, .age] | @csv) | .[]'