Articles
Published · November 18, 2026

Query a JSON File with jq: Filters, Pipes, and Array Basics

To query a JSON file with jq, write a filter: a dot for the whole document, .key for a field, .[] for every array item, and | to chain steps. Here are the basics with short examples.

Also in: Español中文हिन्दी

To query a JSON file with jq, run jq 'FILTER' file.json, where the filter says which part of the document you want. A dot means "everything", .name picks a field, .[] walks every item in an array, and | feeds one step into the next. jq is a command-line JSON processor) that works a bit like AWK or sed, but for structured data.

The examples below all use one small file, notes.json, the kind of export you might keep next to your notes:

[
  {"title": "Groceries", "tags": ["home", "list"], "words": 120},
  {"title": "Trip plan", "tags": ["travel"], "words": 340},
  {"title": "Meeting", "tags": [], "words": 85}
]

Quick answer

Quote the filter in single quotes so your shell leaves it alone, then name the file:

jq '.[0].title' notes.json

That prints "Groceries". Everything else in this article is a variation on that one pattern: a filter, then a file.

Start with the identity filter

In the jq tutorial, a jq program is called a filter, and the simplest one is ., the identity filter, which returns its input unchanged. Run it on the file and you get the same data back, laid out by jq's own formatting:

jq '.' notes.json

The value is unchanged, but the formatting is jq's. That makes it a quick way to read a minified file. If you only want to check that a file parses, our guide to how to pretty-print and validate JSON covers that angle.

Read keys and values

A dot followed by a key name reads that key. According to the jq manual, .foo produces the value at the key "foo" if the key is present, or null otherwise. So a typo gives you null, not an error. Easy to miss:

jq '.[0].missing' notes.json
# null

Add a question mark, as in .foo?, and jq also stays quiet when the input is not an object at all. That helps when a file mixes shapes.

To list the keys of an object, use the keys builtin. It sorts them by Unicode codepoint order:

jq -c '.[0] | keys' notes.json
# ["tags","title","words"]

Array basics: index, slice, iterate

Arrays are zero-based, so .[2] is the third element. Negative numbers count from the end: -1 is the last element, -2 the one before it.

jq '.[-1].title' notes.json
# "Meeting"

A slice such as .[0:2] returns a sub-array, starting at the first index and stopping before the second. The manual's own example is .[2:4] on ["a","b","c","d","e"], which gives ["c","d"].

Leave the index out entirely, as in .[], and jq returns every element, one result at a time:

jq '.[].title' notes.json
# "Groceries"
# "Trip plan"
# "Meeting"

Three separate outputs, not one array. That distinction matters in the next section.

Pipes and commas

The pipe combines two filters by feeding the output of the left one into the input of the right one. .[] | .title means "for each item, take its title". The comma works differently: the same input goes into both filters and their outputs are concatenated in order, so .[0].title, .[2].title prints the first and third titles.

You can build new objects as you pipe. This reshapes each note into a title and a tag count, using the length builtin, which returns the number of elements in an array:

jq -c '.[] | {title: .title, n: (.tags | length)}' notes.json
# {"title":"Groceries","n":2}
# {"title":"Trip plan","n":1}
# {"title":"Meeting","n":0}

The tutorial makes the same point about its own example: the pipeline produces a stream of objects, not an array of them. To get one array back, wrap the whole filter in square brackets:

jq -c '[.[] | .title]' notes.json
# ["Groceries","Trip plan","Meeting"]

Handy builtins: map, select, add, sort_by

map(f) is shorthand for [.[] | f], so the command above can also be written as map(.title). select(f) keeps an input only when f is true and produces no output otherwise. Combine them to filter an array:

jq -c 'map(select(.words > 100) | .title)' notes.json
# ["Groceries","Trip plan"]

add totals numbers, and it joins strings too:

jq 'map(.words) | add' notes.json
# 545

sort_by orders an array by a field, or by any filter you give it:

jq -c 'sort_by(.words) | map(.title)' notes.json
# ["Meeting","Groceries","Trip plan"]

Handy options: -r, -c, and --arg

A handful of command-line flags change how jq reads or prints:

  • -r (--raw-output) writes strings directly, without JSON quotes. Use it when the result goes into a shell script or another tool.
  • -c (--compact-output) puts each result on a single line.
  • --arg name value binds $name as a string. Per the manual, even --arg foo 123 binds the string "123", not a number.
  • -s (--slurp) reads all the input into one array, and -n (--null-input) runs the filter once with null as input.
  • --tab and --indent n change the indentation of pretty-printed output.

Put together, they make a lookup that's safe to script:

jq -r --arg t "Trip plan" '.[] | select(.title == $t) | .words' notes.json
# 340

One more habit: JSON has no comments, so keep notes about your data outside the file. Our article JSON has no comments covers the alternatives.

Common problems and fixes

  • You get `null` instead of a value. The key is missing or misspelled. Check it with keys, and remember that key names are case-sensitive.
  • jq reports an error about indexing. You're using .key on an array, or .[0] on an object. Add .[] to step into the array first, or use .key? to skip mismatches.
  • The shell mangles your filter. Wrap the filter in single quotes. Characters like |, $, and [ mean something to the shell.
  • You get many outputs when you wanted one array. Wrap the filter in [ ], or use map.
  • Strings come out with quotes. Add -r when you want plain text.

Doing this with Carets

jq finds the data. You still have to write, store, and revise the filters and the files they run on. Carets is a fast, native notes and code editor for iPhone, iPad, and Mac, built for Markdown, plain text, and code. Syntax highlighting makes JSON files and shell snippets easier to read, and the files, projects, and tags structure lets you keep notes.json samples and a list of favorite filters in one project. A personal command cheat sheet in plain Markdown is a natural home for the jq lines you reach for most. Your files stay plain text, so they work with jq and any other tool you use.

Download Carets on the App Store for iPhone, iPad, and Mac.

Frequently Asked Questions

What does the dot mean in a jq filter?

A lone dot is the identity filter: it returns its input unchanged, pretty-printed. A dot followed by a key, like .title, reads that key from an object.

How do I get one array instead of many separate objects?

Wrap the filter in square brackets, for example [.[] | .title], or use map(.title). Without the brackets jq prints a stream of separate values.

How do I print a string without quotes?

Use -r (--raw-output). It writes strings directly instead of as quoted JSON strings.

What happens if a key does not exist?

.foo returns null when the key is absent. Use .foo? to avoid an error when the input is not an object.