ToolsWhisper

JSONPath vs jq: Which One to Use (With Side-by-Side Examples)

JSONPath selects values and is built into many tools. jq transforms JSON on the command line. The same queries in both, and how to choose.

Frank4 min read

JSONPath and jq both let you pull values out of a JSON document with a short expression, which is why people confuse them. They are built for different jobs, and picking the right one saves you from forcing a tool to do what it was not designed for.

The one-line answer: JSONPath selects; jq transforms. If you only need to find values, either works. If you need to reshape, count, or compute, you want jq.

The same questions in both

All of these use the classic bookstore document from the original JSONPath article:

{ "store": {
    "book": [
      { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 },
      { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 },
      { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 },
      { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 }
    ],
    "bicycle": { "color": "red", "price": 19.95 }
} }
Question JSONPath jq
All authors $.store.book[*].author .store.book[].author
Titles under 10 $.store.book[?(@.price < 10)].title .store.book[] | select(.price < 10) | .title
Books that have an ISBN $.store.book[?(@.isbn)].title .store.book[] | select(.isbn) | .title
Every price, at any depth $..price .. | .price? // empty
The last book $.store.book[-1:] .store.book[-1]

For the filter on price, JSONPath returns ["Sayings of the Century", "Moby Dick"], and so does the jq version. For selecting, they are close to equivalent, and JSONPath is usually shorter.

Missing fields behave differently

Ask for a key that is not there and the two tools disagree. JSONPath treats it as "no match" and returns an empty list. jq's .isbn on an object without an isbn returns null, and that null then flows on into the rest of your pipeline. It is why a jq query can print null where a JSONPath query would print nothing. The // operator gives jq a default value: .isbn // "none".

What only jq can do

The difference shows up the moment you want to change the output rather than point at it:

# Reshape each book to just title and price
jq '[.store.book[] | {title, price}]' books.json

# Count books
jq '.store.book | length' books.json

# Total price
jq '[.store.book[].price] | add' books.json

JSONPath has no way to build new objects, sum a field or group results. Its expressions return the nodes they matched, unchanged. RFC 9535 does define a handful of functions such as length() and count(), but they exist only inside filter expressions, to test a condition, not to produce output.

Getting clean output from jq

By default jq prints each result as JSON, so strings keep their quotes. Add -r for raw text: jq -r '.store.book[].title' books.json prints one bare title per line, ready for a shell loop. -c prints compact one-line JSON, and -s (slurp) reads the whole input into a single array. JSONPath, by contrast, hands back a list of the values it matched, and what happens next is up to the tool around it.

Passing values into a query

Do not splice shell variables into a jq program by string concatenation. Pass them in:

jq --argjson max 10 '.store.book[] | select(.price < $max) | .title' books.json

--argjson passes a JSON value, here a number, while --arg always passes a string. This also avoids quoting mistakes when the value comes from user input.

Very large files

jq reads the whole document into memory before it runs. For a file too big for that, its --stream mode processes the input as a flow of events instead, although the programs are written differently and take some getting used to. JSONPath implementations generally work on a document that has already been parsed into memory as well, so neither one is a streaming tool by default.

Where JSONPath is the right tool

You will often meet JSONPath where you cannot run jq at all, because it is embedded in a product:

  • API testing tools use it to assert on a response field.
  • API gateways and workflow engines use it to map one value into the next step.
  • kubectl offers -o jsonpath='{.items[*].metadata.name}', though note that kubectl's JSONPath is its own template dialect with extras such as {range}.

In all of those the question is "which field", and a path expression is exactly enough.

The catch: JSONPath dialects differ

jq has one implementation and one manual. JSONPath, for most of its life, was an informal article from 2007, and libraries disagreed about filters, slices and out-of-range behaviour. RFC 9535, published in 2024, finally standardised it. Older libraries still support extras the RFC dropped, such as script expressions and =~ regular expressions, and may handle edge cases differently.

The practical advice: when a JSONPath will run inside a particular product, test it in that product. An expression that works in one library can return nothing in another.

A third option

If you want a standardised query-and-transform language with a formal specification that you can embed, there is also JMESPath, which the AWS CLI uses for its --query option. It sits between the two: more capable than JSONPath for reshaping, easier to embed than jq.

How to choose

  • Use JSONPath to extract a value inside a tool, a test or a config that already speaks it.
  • Use jq in the shell, in scripts and pipelines, and whenever you need to reshape or compute.
  • Use neither for anything complicated in application code. A few lines of ordinary code in your own language is clearer than a clever expression.

To try a JSONPath against your own document, paste both into the JSONPath tester. It shows the matched values and the full normalised path to each one, which is also the quickest way to learn the syntax. If you only need to tidy or validate the document first, use the JSON formatter.

Tools used in this article

Read next