You call an API, get back 4,000 lines of JSON, and you need one field that lives five levels down inside an array. You could write a loop. Or you could write one short expression and be done before your coffee gets cold.
What is JSONPath
JSONPath is a query language for JSON, much as XPath is for XML. One short expression pulls specific values, arrays or objects out of a document. Stefan Goessner proposed it in 2007, and the IETF published it as a standard in 2024 as RFC 9535.
You describe the path to the data, and JSONPath hands back whatever matches. No nested loops, no if (data && data.store && ...) chains.
JSONPath syntax basics
Every expression starts with $, the root of the document. From there you step into the structure with dots and brackets:
| Syntax | Meaning |
|---|---|
$ | The root |
.key | Child by name (dot notation) |
['key'] | Child by name (bracket notation) |
.. | Recursive descent, searching every level |
* | Wildcard: every child |
[n] | Array index, starting at 0 |
[start:end] | Array slice |
[?(@.expr)] | Filter |
@ | The current element, used inside filters |
The examples below use this small bookshop. Every developer bookshelf has at least two of these:
{
"store": {
"name": "Tech Books",
"books": [
{
"title": "Clean Code",
"author": "Robert C. Martin",
"price": 33.99,
"tags": ["programming", "best-practices"]
},
{
"title": "The Pragmatic Programmer",
"author": "David Thomas",
"price": 42.5,
"tags": ["programming", "career"]
},
{
"title": "Design Patterns",
"author": "Gang of Four",
"price": 39.99,
"tags": ["programming", "architecture"]
}
]
}
}Common JSONPath examples
Nested properties
Dot notation walks down the hierarchy, and an index picks an array item:
$.store.name → "Tech Books"
$.store.books[0].title → "Clean Code"
$.store.books[2].price → 39.99Wildcards
* selects every element at that level. On an array it returns one result per item:
$.store.books[*].title → ["Clean Code", "The Pragmatic Programmer", "Design Patterns"]
$.store.books[*].price → [33.99, 42.5, 39.99]Array slices
Slices select a range, like Python: the start is included and the end isn’t. A negative number counts from the end.
$.store.books[0:2].title → ["Clean Code", "The Pragmatic Programmer"]
$.store.books[-1:].title → ["Design Patterns"]Filters
A filter keeps the elements that match a condition, with @ standing for the element being tested:
$.store.books[?(@.price < 40)] → the Clean Code and Design Patterns objects
$.store.books[?(@.author == 'David Thomas')] → the Pragmatic Programmer object
$.store.books[?(@.tags[0] == 'programming')] → all three booksRFC 9535 also allows filters without the parentheses, as in [?@.price < 40]. Most libraries accept the older form shown above.
Recursive descent
.. searches every level of the document. Use it when you know a key’s name but not where it sits:
$..title → ["Clean Code", "The Pragmatic Programmer", "Design Patterns"]
$..price → [33.99, 42.5, 39.99]JSONPath and XPath
JSONPath borrows its ideas from XPath, so if you know one, the other will feel familiar:
| Concept | XPath | JSONPath |
|---|---|---|
| Root | / | $ |
| Child | / | . or [] |
| Recursive descent | // | .. |
| Filter | [condition] | [?(condition)] |
The main difference is the data model. XPath handles XML’s attributes, namespaces and text nodes. JSONPath only has to deal with objects, arrays and values.
How to use the JSONPath tester
- Open the JSONPath tester and paste your JSON into JSON data.
- Type an expression into JSONPath expression, or pick one of the examples under Try.
- Select Evaluate or press Enter. A single match is shown as its value, several matches as an array, and otherwise you see “No matches found.” Invalid JSON shows the parser error.
- Copy the results, or Clear to start again.
The tester runs in your browser and supports the core of JSONPath: $, dot notation, array indexes such as books[0], and wildcards such as books[*] and store.*. That covers the nested property and wildcard examples above. Bracket notation, slices, negative indexes, filters and recursive descent aren’t supported yet. To test those, use a full library such as JSONPath-Plus.
Where JSONPath is used
- API testing tools use it to pull values out of responses for assertions.
- Data pipelines use it to extract fields from large JSON documents.
- Kubernetes’
kubectlaccepts JSONPath templates with-o jsonpath. - Monitoring and automation tools use it to read values from logs and webhook payloads.
Before you put an expression into code or configuration, test it against real sample data so you know it selects what you expect.
