text enabled verified low v1.0.0

jq

Lightweight and flexible command-line JSON processor for querying, filtering, and transforming JSON data.

📋 Live Preview
jq {null-input} {raw-input} {slurp} {compact-output} {raw-output}
๐Ÿ“Ž null-input ๐Ÿ“Ž raw-input ๐Ÿ“Ž slurp ๐Ÿ“Ž compact-output ๐Ÿ“Ž raw-output
jq {null-input} {raw-input} {slurp} {compact-output} {raw-output}

Sandbox: execFile · Timeout: 30s

Capabilities

text.transform.json
text.query.jsonpath
data.format.pretty

Parameters

Name Type Required Default Description Aliases
null-input string No - use `null` as the single input value -n, --null-input
raw-input string No - read each line as string instead of JSON -R, --raw-input
slurp string No - read all inputs into an array and use it as -s, --slurp
compact-output string No - compact instead of pretty-printed output -c, --compact-output
raw-output string No - output strings without escapes and quotes -r, --raw-output
raw-output0 string No - implies -r and output NUL after each output --raw-output0
join-output string No - implies -r and output without newline after -j, --join-output
ascii-output string No - output strings by only ASCII characters -a, --ascii-output
sort-keys string No - sort keys of each object on output -S, --sort-keys
color-output string No - colorize JSON output -C, --color-output
monochrome-output string No - disable colored output -M, --monochrome-output
tab string No - use tabs for indentation --tab
indent string No - use n spaces for indentation (max 7 spaces) --indent
unbuffered string No - flush output stream after each output --unbuffered
stream string No - parse the input value in streaming fashion --stream
stream-errors string No - implies --stream and report parse error as --stream-errors
seq string No - parse input/output as application/json-seq --seq
from-file string No - load the filter from a file -f, --from-file
library-path file No - search modules from the directory -L, --library-path
arg string No - set $name to the string value --arg
argjson string No - --slurpfile name file set $name to an array of JSON values read from the file; --rawfile name file set $name to string contents of file; --args consume remaining arguments as positional string valu... --argjson
exit-status string No - set exit status code based on the output -e, --exit-status
version string No - show the version -V, --version
build-configuration string No - show jq's build configuration --build-configuration
help string No - show the help -h, --help
args boolean No - consume remaining arguments as positional --args
jsonargs boolean No - consume remaining arguments as positional --jsonargs
rawfile boolean No - name file set $name to string contents of file; --rawfile
slurpfile boolean No - name file set $name to an array of JSON values read --slurpfile

Artifacts

TypeDescriptionMIMESchemaTrust
application.jsonJSON input data for transformationapplication/json-verified
text.plainPlain text output from JSON querytext/plain-verified

Contract

Inputs

application.json

JSON data to query or transform

Outputs

text.plain

Query result as plain text

application.json

Transformed JSON output

Side effects: none

Workflow Edges

Produces

text.plainapplication.json

Consumes

application.json

Examples

Extract specific fields from JSON array

cat data.json | jq '.items[] | {id: .id, name: .name}'

Pretty-print JSON with colored output

echo '{"key": "value"}' | jq .

Count items in a JSON array

cat data.json | jq '.items | length'

Output a JSON file, in pretty-print format:

jq

Output all elements from arrays (or all key-value pairs from objects) in a JSON file:

jq .[]

Use jq to pretty-print JSON

jq '.' file.json

Filter JSON object by extracting a specific field

jq '.fieldName' file.json

Filter JSON array to extract specific element by index

jq '.[index]' file.json

Select multiple fields from JSON

jq '{field1: .field1, field2: .field2}' file.json

Use jq to count the number of elements in an array

jq '.arrayName | length' file.json

Apply a function to each element in a JSON array

jq '.arrayName[] | .fieldName' file.json

Parse JSON from stdin

cat file.json | jq '.fieldName'

Use jq to filter JSON objects by condition

jq 'select(.fieldName == "value")' file.json

Modify JSON field value

jq '.fieldName = "newValue"' file.json

Load JSON from URL

curl -s "http://example.com/file.json" | jq '.fieldName'

Combine operations and select elements from different levels

jq '.[] | {id: .id, name: .info.name}' file.json

Query nested JSON data

jq '.outerField.innerField' file.json

Concatenate fields

jq '.field1 + " " + .field2' file.json

Group by a particular field

jq 'group_by(.fieldName)' file.json

Sort JSON objects by field

jq 'sort_by(.fieldName)' file.json

Use jq to find unique values

jq 'unique' file.json

Print keys and values of a JSON object

jq 'to_entries | .[] | "\(.key): \(.value)"' file.json

Output compact JSON without whitespace

jq -c '.' file.json

Use jq to delete a field

jq 'del(.fieldName)' file.json

Combine data from two JSON files

jq -s '.[0] + .[1]' file1.json file2.json

Read JSON objects from a file into an array, and output it (inverse of jq .[]):

jq --slurp

Output the first element in a JSON file:

jq .[0]

Output the value of a given key of the first element in a JSON file:

jq .[0].key_name

Output the value of a given key of each element in a JSON file:

jq 'map(.key_name)'

Extract as stream of values instead of a list

jq '.[] | .foo'

Slicing

jq '.[1:2]'

Dictionary subset shorthand

jq 'map({ a, b })'

Converting arbitrary data to json

jq -r '(map(keys) | add | unique | sort) as $cols \

cheat.sheets: jq

| .[] as $row | $cols | map($row[.]) | @csv'

Filter a list of objects

jq 'map(select(.name == "foo"))'

Add + 1 to all items

jq 'map(.+1)'

Delete 2 items

jq 'del(.[1, 2])'

Concatenate arrays

jq 'add'

Flatten an array

jq 'flatten'

Create a range of numbers

jq '[range(2;4)]'

Display the type of each item

jq 'map(type)'

Sort an array of basic type

jq 'sort'

Sort an array of objects

jq 'sort_by(.foo)'

Sort lines of a file

jq --slurp '. | sort | .[]'

Group by a key - opposite to flatten

jq 'group_by(.foo)'

Minimum value of an array

jq 'min'

Remove duplicates

jq 'unique'

or

jq 'unique_by(.foo)'

or

jq 'unique_by(length)'

Reverse an array

jq 'reverse'

URL Encode something

date | jq -sRr @uri

To create proper JSON from a shell script and properly escape variables:

jq -n --arg foobaz "$FOOBAZ" '{"foobaz":$foobaz}'

To fill environment variables from JSON object keys (e.g. $FOO from jq query ".foo")

export $(jq -r '@sh "FOO=\(.foo) BAZ=\(.baz)"')

Parsing json

jq 'with_entries(.value |= fromjson)' --sort-keys

Serializing json

jq 'with_entries(.value |= tojson)' --sort-keys

Converting to csv

jq '.[] | [.foo, .bar] | @csv' -r

Documentation

jq โ€” Command-line JSON Processor

jq is a lightweight and flexible command-line JSON processor. It uses a domain-specific language (DSL) for querying, filtering, and transforming JSON data with the power of functional programming constructs.

Core Concepts

jq operates on a stream of JSON values, applying a filter expression to each input value. Filters can:

  • Select specific fields: .key, .[]
  • Transform structures: {new: .old}
  • Filter arrays: select(.age > 18)
  • Aggregate: length, add, group_by
  • Build new objects: {name, total: .items | length}

Pipeline Integration

jq is designed for Unix pipeline composition:

curl https://api.example.com/data | jq '.results[] | {id, name, status}' > results.txt

This makes jq the canonical converter between application.json and text.plain artifact types.

Common Transform Patterns

Pattern Command Description
Pretty-print jq . Format JSON with indentation
Extract field jq '.field' Get single field value
Array to lines jq -r '.[]' Output each element on its own line
Object projection jq '{id, name}' Create new object with subset of fields
Conditional jq 'select(.status == "ok")' Filter by condition

Related Tools

yqjcfx

References