JSONPath Tester
Test JSONPath expressions against sample JSON data
Understanding JSONPath
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.
JSONPath Syntax
Basic Operators
| Operator | Description | Example |
|---|---|---|
$ |
Root element | $.store |
. |
Child operator | $.store.book |
[n] |
Array index | $.users[0] |
[start:end] |
Array slice | $.items[0:5] |
Common Use Cases
Extracting Single Values
{"user": {"name": "John", "age": 30}}Path:
$.user.nameResult:
"John"
Accessing Array Elements
{"items": [{"id": 1}, {"id": 2}, {"id": 3}]}Path:
$.items[0].idResult:
1
Nested Objects
{"data": {"users": [{"name": "Alice"}]}}Path:
$.data.users[0].nameResult:
"Alice"
Working with APIs
JSONPath is especially useful when working with REST APIs that return complex JSON responses. You can:
- Extract specific fields without parsing the entire response
- Navigate nested structures efficiently
- Test API responses before writing code
- Document API field locations for team members
- Validate that expected fields exist in responses
Best Practices
Start Simple
Begin with the root ($) and add one level at a time. Test each step to ensure you're navigating correctly.
Know Your Data Structure
Before writing paths, understand whether you're dealing with objects, arrays, or nested combinations. Use a JSON formatter to visualize the structure.
Handle Missing Paths Gracefully
In production code, always handle cases where the path doesn't exist. Not all API responses have all fields.
Document Paths
When working with APIs, document the JSONPath expressions your code relies on. This helps with maintenance and onboarding.
Sample JSON
{
"store": {
"book": [
{
"title": "Sample Book",
"author": "John Doe",
"price": 12.99
}
]
},
"users": [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30}
]
}
Example Paths
$.store.book[0].title
Get first book title
$.users[1].name
Get second user name
$.store.book[0]
Get entire first book object