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

Indexing, slicing, mapping, and searching arrays.

First array item

Get the first element.

bashANYjqarraysfirst
bash
echo '[10,20,30]' | jq 'first'
Notes

`first` returns the first item from an array or stream.

Last array item

Get the last element.

bashANYjqarrayslast
bash
echo '[10,20,30]' | jq 'last'
Notes

`last` returns the final item.

Nth item from array

Get a specific array element by index.

bashANYjqarraysnth
bash
echo '[10,20,30]' | jq 'nth(1)'
Notes

Useful when you prefer function form over indexing syntax.

Find item index

Find the index of an element in an array.

bashANYjqarraysindex
bash
echo '["a","b","c"]' | jq 'index("b")'
Notes

Returns the first matching index or null.

Check array contains value

Test membership in an array.

bashANYjqcontainsarrays
bash
echo '["a","b","c"]' | jq 'contains(["b"])'
Notes

`contains()` works for arrays and objects.

Check whether any item matches

Return true if any element satisfies a predicate.

bashANYjqanyarrays
bash
echo '[{"active":false},{"active":true}]' | jq 'any(.[]; .active)'
Notes

`any()` is concise for boolean checks across collections.

Check whether all items match

Return true if every element satisfies a predicate.

bashANYjqallarrays
bash
echo '[{"active":true},{"active":true}]' | jq 'all(.[]; .active)'
Notes

`all()` verifies complete compliance across the array.

Reverse array

Reverse the order of array items.

bashANYjqreversearrays
bash
echo '[1,2,3,4]' | jq 'reverse'
Notes

Useful for display order and last-first style reports.

Object Basics

Inspect object fields and values.

Object values

Return all object values as an array.

bashANYjqobjectsvalues
bash
echo '{"a":1,"b":2}' | jq 'values'
Notes

Use when you want to ignore keys and work with values only.

Keys unsorted

Return keys preserving insertion semantics when possible.

bashANYjqobjectskeys
bash
echo '{"b":2,"a":1}' | jq 'keys_unsorted'
Notes

Useful when you want original key order rather than sorted order.

Get JSON type

Return the type of the current value.

bashANYjqtypeobjects
bash
echo '{"a":1}' | jq 'type'
Notes

Returns values like `object`, `array`, `string`, `number`, and `boolean`.

Project specific fields

Build a smaller object from chosen keys.

bashANYjqobjectsprojection
bash
echo '{"id":1,"name":"alice","email":"a@example.com","role":"admin"}' | jq '{id, name, role}'
Notes

Ideal for reducing noisy API payloads.

Build nested object

Construct nested output structures.

bashANYjqobjectsconstruct
bash
echo '{"first":"Alice","last":"Ng","team":"eng"}' | jq '{user: {full_name: (.first + " " + .last)}, meta: {team}}'
Notes

jq object construction supports arbitrary expressions as values.

Check object contains subset

Test whether an object includes certain key-value pairs.

bashANYjqcontainsobjects
bash
echo '{"role":"admin","active":true}' | jq 'contains({role: "admin"})'
Notes

Very useful for policy and config checks.

Entries and Key-Value Maps

Turn objects into entry arrays and back again.

Map object values

Transform each value while keeping keys unchanged.

bashANYjqmap-valuesobjects
bash
echo '{"a":1,"b":2}' | jq 'map_values(. * 10)'
Notes

`map_values()` is concise when only object values need changing.

Filter object entries by value

Remove entries below a threshold.

bashANYjqentriesfiltering
bash
echo '{"a":1,"b":3,"c":5}' | jq 'to_entries | map(select(.value >= 3)) | from_entries'
Notes

A classic pattern for object filtering.

Rename keys with entries

Rename object keys during transformation.

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

Great for adapting between naming conventions.

Create object from array items

Index objects by a field.

bashANYjqentrieslookup
bash
echo '[{"id":"u1","name":"alice"},{"id":"u2","name":"bob"}]' | jq 'map({key: .id, value: .name}) | from_entries'
Notes

Convenient for building lookup tables.

Create grouped object

Group items by key and emit object buckets.

bashANYjqgroup-byentries
bash
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'
Notes

Excellent for reporting and reshaping lists into keyed groups.

Paths and Deep Updates

Edit or inspect nested structures by path.

Read value by explicit path

Look up a nested value using a path array.

bashANYjqpathsgetpath
bash
echo '{"user":{"profile":{"name":"alice"}}}' | jq 'getpath(["user","profile","name"])'
Notes

Useful when paths are built dynamically.

Set value by explicit path

Write a nested value using a path array.

bashANYjqpathssetpath
bash
echo '{}' | jq 'setpath(["user","profile","name"]; "alice")'
Notes

`setpath()` can create intermediate objects as needed.

Delete multiple paths

Remove several nested paths at once.

bashANYjqpathsdelete
bash
echo '{"user":{"password":"secret","token":"abc","name":"alice"}}' | jq 'delpaths([["user","password"],["user","token"]])'
Notes

Very useful for scrubbing sensitive fields.

Find all scalar paths

List paths that end in scalar values.

bashANYjqpathsscalars
bash
echo '{"user":{"name":"alice","roles":["admin","dev"]}}' | jq 'paths(scalars)'
Notes

Great for data profiling and flattening strategies.

Get all leaf values

Return every scalar value in a document.

bashANYjqrecursivescalars
bash
echo '{"user":{"name":"alice","roles":["admin","dev"]}}' | jq '.. | scalars'
Notes

Recursive descent plus `scalars` is a good way to inspect content quickly.

Find matching nested objects

Search recursively for objects containing a field.

bashANYjqrecursivesearch
bash
echo '{"items":[{"name":"a"},{"child":{"name":"b"}}]}' | jq '.. | objects | select(has("name"))'
Notes

The recursive descent operator `..` is powerful for document-wide search.

Grouping and Aggregation

Summaries, counts, and report-style reductions.

Count items by field

Build a frequency table from array objects.

bashANYjqgroup-bycount
bash
echo '[{"status":"ok"},{"status":"fail"},{"status":"ok"}]' | jq 'group_by(.status) | map({status: .[0].status, count: length})'
Notes

One of the most useful jq reporting patterns.

Sum numeric field

Sum one field across an array of objects.

bashANYjqsumaggregation
bash
echo '[{"amount":10},{"amount":15},{"amount":5}]' | jq 'map(.amount) | add'
Notes

Map to the field first, then aggregate.

Average numeric field

Compute an average across objects.

bashANYjqaverageaggregation
bash
echo '[{"score":80},{"score":90},{"score":100}]' | jq '(map(.score) | add) / length'
Notes

Simple but common analytics pattern.

Min and max by field

Find objects with smallest and largest values.

bashANYjqmin-bymax-by
bash
echo '[{"name":"a","age":20},{"name":"b","age":35}]' | jq '{youngest: min_by(.age), oldest: max_by(.age)}'
Notes

`min_by()` and `max_by()` are very handy for summaries.

Partition items by predicate

Split one array into matching and non-matching groups.

bashANYjqpartitionarrays
bash
echo '[{"name":"a","active":true},{"name":"b","active":false}]' | jq '{active: map(select(.active)), inactive: map(select(.active | not))}'
Notes

Useful for dashboards and summary reports.

Recommended next

No recommendations yet.