jq
Syntax for filtering, mapping, transforming, and restructuring JSON data from the command line.
Basic Extraction & Formatting
Essential commands to format, pretty-print, extract keys, and work with raw text or indices.
Pretty Print JSON
Formats JSON with indentation for human-readable output.
jq '.'cat data.json | jq '.'Extract Key Value
Selects the value of a specific key in a JSON object.
jq '.<key_name>'jq '.name' data.jsonOutput Raw Text
Outputs string values without surrounding double quotes for scripting.
jq -r '.<key_name>'jq -r '.name' data.jsonAccess Array Index
Retrieves a specific element from an array by its numeric index.
jq '.[<index>]'jq '.[0]' items.jsonSlice Array Range
Extracts a sub-range of elements from an array.
jq '.[<start>:<end>]'jq '.[1:3]' items.jsonFiltering, Length & Conditionals
Methods to search, evaluate conditions, check sizes, and apply conditional logic.
Select with Condition
Filters objects based on a conditional expression.
jq 'select(.<key> <operator> <value>)'jq 'select(.age > 18)' users.jsonCheck Length
Returns the length of an array, string, or object.
jq '.<key> | length'jq '.tags | length' post.jsonConditional If Else
Transforms values conditionally using if-then-else logic.
jq 'if .<key> then <true_expr> else <false_expr> end'jq 'if .active then "Active" else "Inactive" end' data.jsonMapping and Restructuring
Transforming arrays, projecting new objects, and flattening data structures.
Map Over Array
Applies an expression to each element in an array.
jq 'map(.<key>)'jq 'map(.name)' users.jsonConstruct New Object
Builds a custom JSON object with selected properties.
jq '{<new_key>: .<old_key>}'jq '{fullName: .name, userAge: .age}' user.jsonMap Object Fields
Creates an array of custom-shaped objects from an array.
jq 'map({<key1>: .<prop1>, <key2>: .<prop2>})'jq 'map({name: .name, age: .age})' users.jsonFlatten Nested Arrays
Flattens an array of arrays into a single-level array.
flattenjq 'flatten' nested.jsonAdvanced Transformations & Aggregations
Powerful aggregations, string interpolation, grouping, conversion, and variables.
String Interpolation
Formats strings dynamically using object variables.
jq '"<text> \(.<key>) <text>"'jq '"Hello \\(.name)!"' user.jsonGroup By Key
Groups objects in an array by a specific key.
jq 'group_by(.<key>)'jq 'group_by(.category)' products.jsonCount Value Occurrences
Counts occurrences of unique values within an array.
jq 'group_by(.) | map({key: .[0], count: length})'jq '["a", "b", "a", "c"] | group_by(.) | map({key: .[0], count: length})'Convert JSON to CSV
Converts an array of objects into flat CSV rows.
jq -r 'map([.<prop1>, .<prop2>] | @csv) | .[]'jq -r 'map([.name, .age] | @csv) | .[]' users.jsonPass External Variables
Safely passes external shell variables into jq scripts.
jq --arg <var_name> "<value>" '.<key> == $var_name'jq --arg target "Admin" '.role == $target' users.json