jq Arrays and Objects

Deep reference for jq arrays, objects, grouping, entries, path updates, reshaping, and structural manipulation.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Array Basics
First array item
echo '[10,20,30]' | jq 'first'

# Get the first element.

Last array item
echo '[10,20,30]' | jq 'last'

# Get the last element.

Nth item from array
echo '[10,20,30]' | jq 'nth(1)'

# Get a specific array element by index.

Find item index
echo '["a","b","c"]' | jq 'index("b")'

# Find the index of an element in an array.

Check array contains value
echo '["a","b","c"]' | jq 'contains(["b"])'

# Test membership in an array.

Check whether any item matches
echo '[{"active":false},{"active":true}]' | jq 'any(.[]; .active)'

# Return true if any element satisfies a predicate.

Check whether all items match
echo '[{"active":true},{"active":true}]' | jq 'all(.[]; .active)'

# Return true if every element satisfies a predicate.

Reverse array
echo '[1,2,3,4]' | jq 'reverse'

# Reverse the order of array items.

## Object Basics
Object values
echo '{"a":1,"b":2}' | jq 'values'

# Return all object values as an array.

Keys unsorted
echo '{"b":2,"a":1}' | jq 'keys_unsorted'

# Return keys preserving insertion semantics when possible.

Get JSON type
echo '{"a":1}' | jq 'type'

# Return the type of the current value.

Project specific fields
echo '{"id":1,"name":"alice","email":"a@example.com","role":"admin"}' | jq '{id, name, role}'

# Build a smaller object from chosen keys.

Build nested object
echo '{"first":"Alice","last":"Ng","team":"eng"}' | jq '{user: {full_name: (.first + " " + .last)}, meta: {team}}'

# Construct nested output structures.

Check object contains subset
echo '{"role":"admin","active":true}' | jq 'contains({role: "admin"})'

# Test whether an object includes certain key-value pairs.

## Entries and Key-Value Maps
Map object values
echo '{"a":1,"b":2}' | jq 'map_values(. * 10)'

# Transform each value while keeping keys unchanged.

Filter object entries by value
echo '{"a":1,"b":3,"c":5}' | jq 'to_entries | map(select(.value >= 3)) | from_entries'

# Remove entries below a threshold.

Rename keys with entries
echo '{"first_name":"Alice","last_name":"Ng"}' | jq 'to_entries | map(.key |= gsub("_"; "-")) | from_entries'

# Rename object keys during transformation.

Create object from array items
echo '[{"id":"u1","name":"alice"},{"id":"u2","name":"bob"}]' | jq 'map({key: .id, value: .name}) | from_entries'

# Index objects by a field.

Create grouped object
echo '[{"team":"eng","name":"a"},{"team":"eng","name":"b"},{"team":"ops","name":"c"}]' | jq 'group_by(.team) | map({key: .[0].team, value: map(.name)}) | from_entries'

# Group items by key and emit object buckets.

## Paths and Deep Updates
Read value by explicit path
echo '{"user":{"profile":{"name":"alice"}}}' | jq 'getpath(["user","profile","name"])'

# Look up a nested value using a path array.

Set value by explicit path
echo '{}' | jq 'setpath(["user","profile","name"]; "alice")'

# Write a nested value using a path array.

Delete multiple paths
echo '{"user":{"password":"secret","token":"abc","name":"alice"}}' | jq 'delpaths([["user","password"],["user","token"]])'

# Remove several nested paths at once.

Find all scalar paths
echo '{"user":{"name":"alice","roles":["admin","dev"]}}' | jq 'paths(scalars)'

# List paths that end in scalar values.

Get all leaf values
echo '{"user":{"name":"alice","roles":["admin","dev"]}}' | jq '.. | scalars'

# Return every scalar value in a document.

Find matching nested objects
echo '{"items":[{"name":"a"},{"child":{"name":"b"}}]}' | jq '.. | objects | select(has("name"))'

# Search recursively for objects containing a field.

## Grouping and Aggregation
Count items by field
echo '[{"status":"ok"},{"status":"fail"},{"status":"ok"}]' | jq 'group_by(.status) | map({status: .[0].status, count: length})'

# Build a frequency table from array objects.

Sum numeric field
echo '[{"amount":10},{"amount":15},{"amount":5}]' | jq 'map(.amount) | add'

# Sum one field across an array of objects.

Average numeric field
echo '[{"score":80},{"score":90},{"score":100}]' | jq '(map(.score) | add) / length'

# Compute an average across objects.

Min and max by field
echo '[{"name":"a","age":20},{"name":"b","age":35}]' | jq '{youngest: min_by(.age), oldest: max_by(.age)}'

# Find objects with smallest and largest values.

Partition items by predicate
echo '[{"name":"a","active":true},{"name":"b","active":false}]' | jq '{active: map(select(.active)), inactive: map(select(.active | not))}'

# Split one array into matching and non-matching groups.

Recommended next

No recommendations yet.