Test JSONPath expressions against sample JSON data
JSONPath is a query language for JSON, similar to XPath for XML. It allows you to extract specific values from complex JSON structures using path expressions.
| Operator | Description | Example |
|---|---|---|
$ |
Root element | $.store |
. |
Child operator | $.store.book |
[n] |
Array index | $.users[0] |
[start:end] |
Array slice | $.items[0:5] |
{"user": {"name": "John", "age": 30}}$.user.name"John"
{"items": [{"id": 1}, {"id": 2}, {"id": 3}]}$.items[0].id1
{"data": {"users": [{"name": "Alice"}]}}$.data.users[0].name"Alice"
JSONPath is especially useful when working with REST APIs that return complex JSON responses. You can:
Begin with the root ($) and add one level at a time. Test each step to ensure you're navigating correctly.
Before writing paths, understand whether you're dealing with objects, arrays, or nested combinations. Use a JSON formatter to visualize the structure.
In production code, always handle cases where the path doesn't exist. Not all API responses have all fields.
When working with APIs, document the JSONPath expressions your code relies on. This helps with maintenance and onboarding.
{
"store": {
"book": [
{
"title": "Sample Book",
"author": "John Doe",
"price": 12.99
}
]
},
"users": [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30}
]
}
$.store.book[0].title
Get first book title
$.users[1].name
Get second user name
$.store.book[0]
Get entire first book object