What Is JSONPath and How Do You Use It?

Big JSON documents are hard to read. Often you only need one value. Or you need one field from every item in a list. Scrolling through hundreds of lines to find it is slow, and it is easy to make mistakes.

JSONPath helps with this. It is a short way to point at the values you want. This guide uses one small sample document. It shows many queries and what each one returns.

Quick answer: JSONPath is a query language for JSON. You write a short expression, like $.items[*].name. It returns the matching values from a document. $ is the whole document. .key picks a field. [0] picks one list item, and [*] picks all of them.

What is JSONPath?

A query language is a small language for asking questions about data. JSONPath does this for JSON. A query or expression is one question you write in it.

JSONPath started as an idea in a 2007 blog post by Stefan Goessner. In 2024 it became an official standard, RFC 9535.

Many tools were made before the standard existed. So small details can be different from tool to tool. The basics in this guide work almost everywhere.

The sample document

All the examples below use this JSON:

{
  "shop": "Corner Store",
  "items": [
    { "name": "Pen", "price": 2, "tags": ["office"] },
    { "name": "Notebook", "price": 12, "tags": ["office", "paper"] },
    { "name": "Backpack", "price": 45, "stock": 0 }
  ],
  "owner": { "name": "Sam", "email": "sam@example.com" }
}

JSONPath syntax: the basic parts

  • $ — the root. This means the whole document. Every expression starts here.
  • .key — the value of a key, like .shop.
  • ['key'] — the same thing in bracket form. Use it when a key has spaces or dashes, like ['first name'].
  • [0] — one item in a list (array). Counting starts at 0.
  • [*] — every item in a list, or every value in an object. This is called a wildcard.
  • [0:2] — a slice (a part of a list): items 0 and 1. The end number is not included.
  • .. — search at every level. This is called recursive descent.
  • [?( … )] — a filter. It keeps only the items that pass a test.
  • @ — inside a filter, the item being tested right now.

JSONPath examples with results

Picking single values

$.shop                 →  "Corner Store"
$.owner.email          →  "sam@example.com"
$['owner']['email']    →  "sam@example.com"
$.items[0].name        →  "Pen"
$.items[2].price       →  45

$.items[0] on its own returns the whole first item: { "name": "Pen", "price": 2, "tags": ["office"] }.

The standard also allows [-1] for the last item. But some older tools don’t support it.

Picking many values with [*] and slices

$.items[*].name        →  "Pen", "Notebook", "Backpack"
$.items[*].price       →  2, 12, 45
$.items[0:2].name      →  "Pen", "Notebook"
$.owner.*              →  "Sam", "sam@example.com"

Look at $.items[*].price. It returns one price for each item. This is a quick way to get one column from a list.

Searching at every level with ..

$..name                →  "Pen", "Notebook", "Backpack", "Sam"
$..price               →  2, 12, 45
$..tags[0]             →  "office", "office"

$..name finds every name key, wherever it is. That includes the owner’s name, not just the items.

This is useful when you don’t know exactly where a field is. The order of the results can be a little different between tools.

Filtering with [?(@...)]

$.items[?(@.price > 10)].name     →  "Notebook", "Backpack"
$.items[?(@.price <= 10)].name    →  "Pen"
$.items[?(@.stock == 0)].name     →  "Backpack"

Read [?(@.price > 10)] as “keep each item where the price is more than 10”.

You can compare with ==, !=, <, <=, > and >=. The standard also lets you join tests with && (and) and || (or).

The third filter only matches the backpack. The pen and the notebook have no stock key. So the test is false for them.

You may also see filters without the round brackets, like [?@.price > 10]. RFC 9535 uses this form. Many older tools need the round brackets, and the standard still accepts them.

How to test JSONPath expressions

The fastest way to learn is to try queries on your own data. Paste your JSON into the JSONPath Tester and type an expression.

The matches change as you type. You can show the values, the paths, or both. Filters run in a safe, closed space, not as JavaScript. So an expression you copied from somewhere else can’t run harmful code.

Two other tools help you write the expression in the first place:

  • JSON Search finds a key or value. It shows the full path to each match, like $.owner.email. You can paste that path into the tester and build from it.
  • The JSON Viewer shows the document as a tree you can open and close. Then you can see the shape of the data before you write a query.

Where is JSONPath used?

  • API testing. Many tools for testing and watching APIs use JSONPath. You write a path to pick a value from a response and check it.
  • Kubernetes. The kubectl command can print fields with a JSONPath template:
    kubectl get pods -o jsonpath='{.items[*].metadata.name}'
    Note that kubectl puts the expression inside curly braces. The $ at the start is optional there.
  • Code. Most programming languages have JSONPath libraries.

JSONPath vs JMESPath

Be careful with AWS. The AWS CLI’s --query option uses JMESPath, not JSONPath. The two look alike, but they are different languages.

For example, JMESPath expressions don’t start with $, and filters are written in a different way. A JSONPath expression often won’t work where JMESPath is expected.

Some other AWS services, such as Step Functions, use JSONPath-style paths like $.detail. Always check which language a tool expects.

Common questions

What does $ mean in JSONPath?

$ is the root: the whole JSON document. Every JSONPath expression starts from it.

What does .. mean in JSONPath?

.. searches at every level of the document. $..price finds every price key, no matter how deep it is.

What is the difference between JSONPath and JMESPath?

They are two separate query languages for JSON. JSONPath starts with $ and is used by tools like kubectl. The AWS CLI uses JMESPath. An expression for one usually needs changes to work in the other.

Is JSONPath a standard?

Yes. Since 2024 it has an official standard, RFC 9535. Older tools may follow earlier rules that are a little different. So test your expression in the tool you plan to use.