JSONPath is a query language for JSON documents, analogous to XPath for XML. It lets you extract the data you need from nested structures without writing loops in JavaScript or Python. It is especially useful when working with API responses and configurations.
What is JSONPath
JSONPath was proposed by Stefan Gössner in 2007 as a lightweight XPath for JSON. Unlike XPath, JSONPath works with objects, arrays, and JavaScript primitive types.
For example, suppose you have a JSON API response and need to get the names of all authors from a list of articles. The JSONPath query $.articles[*].author does this in one line.
Basic JSONPath syntax
| Operator | Meaning | Example |
|---|---|---|
$ | Document root | $ |
.key | Child element | $.name |
..key | Recursive search | $..name |
[*] | All array elements | $.items[*] |
[n] | Element by index | $.items[0] |
[1,3] | Multiple indices | $.items[1,3] |
[1:4] | Slice | $.items[1:4] |
[-1] | Last element | $.items[-1] |
[?(@.price < 10)] | Filter | $.items[?(@.price < 10)] |
Practical examples
Consider this sample JSON:
{
"store": {
"books": [
{ "title": "CSS Secrets", "author": "Lea Verou", "price": 29.99 },
{ "title": "You Don't Know JS", "author": "Kyle Simpson", "price": 0 },
{ "title": "Clean Code", "author": "Robert Martin", "price": 34.99 }
],
"name": "Bookstore"
}
}Get all books:
$.store.books[*]Get titles only:
$.store.books[*].title**Find books that cost less than 30: **
$.store.books[?(@.price < 30)]Recursively find all fields named author:
$..authorLast book in the list:
$.store.books[-1]Slice — the first two books:
$.store.books[0:2]Filters and conditions
Filters are the most powerful part of JSONPath. Syntax: [?(<выражение>)], where @ represents the current element.
$.books[?(@.price > 0 && @.author == "Kyle Simpson")]Supported operators: ==, !=, <, <=, >, >=, =~ (regular expressions in some implementations).
Where JSONPath is used
Postman — JSONPath queries in tests to extract values from API responses.
Kubernetes — JSONPath in kubectl:
kubectl get pods -o jsonpath='{.items[*].metadata.name}'AWS — conditions in IAM policies and Step Functions:
"InputPath": "$.order.items[?(@.quantity > 0)]"Grafana, Prometheus — extracting metrics from JSON responses.
JSONPath vs. JMESPath vs. jq
| Tool | Syntax | Use |
|---|---|---|
| JSONPath | $.items[*].name | Postman, Kubernetes, Spring |
| JMESPath | items[*].name | AWS CLI, Ansible |
| jq | .items[] | .name | Terminal, scripts |
To format and validate JSON documents before applying queries, use JSON Formatter. To validate a data schema, use JSON Schema Validator.
Tool on reChecker
The JSONPath tester on reChecker lets you enter JSON and a JSONPath expression to instantly see the selection result. It is convenient for debugging API queries without writing code.