JSONPath Query Guide: Precisely Extract Nested Data from JSON
What Is JSONPath
JSONPath is a JSON query language, similar to XPath for XML, that precisely extracts data from complex nested JSON:
{
"store": {
"books": [
{"title": "The Three-Body Problem", "price": 68},
{"title": "To Live", "price": 45},
{"title": "One Hundred Years of Solitude", "price": 55}
]
}
}
Use $.store.books[*].title to extract all titles: ["The Three-Body Problem", "To Live", "One Hundred Years of Solitude"]
Basic Syntax Reference
| Syntax | Meaning | Example |
|---|---|---|
$ |
Root node | $ |
. |
Child node | $.store |
[] |
Array index or child node | $[0], $['key'] |
* |
Wildcard, all elements | $.store.* |
.. |
Recursive descent, all levels | $..title |
[start:end] |
Array slice | $[0:2] |
[?(expr)] |
Filter expression | $[?(@.price > 50)] |
Syntax Explained in Detail
Root Node $
$ represents the JSON root. All paths start from $:
$ → The entire JSON
$.store → The store object
Child Node Access .
Use . or [] to access child nodes:
$.store.books → The books array
$.store['books'] → Equivalent form; required when key names contain special characters
Array Index []
$.store.books[0] → First book
$.store.books[-1] → Last book
$.store.books[0:2] → First two books (slice)
Wildcard *
$.store.books[*] → All books
$.store.books[*].title → All titles
Recursive Descent ..
.. searches all levels without needing the full path:
$..title → All title fields at any level
$..price → All price fields at any level
..is especially useful in deeply nested API responses.
Filter Expressions ?()
Filtering is JSONPath's most powerful feature:
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
== |
Equals | $[?(@.price == 45)] |
!= |
Not equals | $[?(@.price != 45)] |
> |
Greater than | $[?(@.price > 50)] |
< |
Less than | $[?(@.price < 50)] |
>= |
Greater than or equal | $[?(@.price >= 55)] |
Logical Operators
$[?(@.price > 50 && @.price < 70)] → Price between 50 and 70
$[?(@.category == 'fiction' || @.price < 40)] → Fiction or price under 40
String Matching
$[?(@.title =~ /Three.*/)] → Title starting with "Three"
Using the JSONPath Tool
Step 1: Open the Tool
Visit the JSONPath tool to enter the query interface.
Step 2: Enter JSON Data
Paste your API response or JSON data into the input box. You can prettify it first with JSON Formatter.
Step 3: Write a JSONPath Expression
Enter an expression in the query box, e.g. $.data.users[*].name.
Step 4: View Extraction Results
The tool displays matching results in real time and highlights matched nodes in the source.
Practical: Extracting Data from API Responses
GitHub API Example
{
"items": [
{"full_name": "vuejs/vue", "stargazers_count": 45000, "language": "TypeScript"},
{"full_name": "facebook/react", "stargazers_count": 42000, "language": "JavaScript"}
]
}
$.items[*].full_name → All repo names
$.items[?(@.stargazers_count > 43000)].full_name → Repo names with Stars > 43000
Paginated Data Extraction
$.data.list[*].id → All IDs on the current page
$.pagination.next_cursor → Next page cursor
JSONPath vs JMESPath
| Comparison | JSONPath | JMESPath |
|---|---|---|
| Syntax style | XPath-like | SQL/pipe-like |
| Recursive search | .. natively supported |
Requires explicit traversal |
| Filter syntax | [?(@.x > 1)] |
[?x > 1] |
| Projection/transform | Not supported | Supports [*].{n: name} |
| Multi-result sorting | Not guaranteed | Can sort |
| Tool | JSONPath | JMESPath |
Use JSONPath for simple extraction, JMESPath for transformation/projection.
Common Issues
| Issue | Cause | Solution |
|---|---|---|
| Query returns empty | Path typo | Check key name casing |
| Filter not working | Syntax version difference | Confirm the tool's supported syntax version |
| Array out of bounds | Index exceeds range | Use [*] or check array length |
| Too many recursive results | .. matched all levels |
Narrow scope or add filter conditions |
| Special character key error | Key contains . or - |
Use bracket syntax ['key.name'] |
Summary
JSONPath is a powerful tool for working with JSON data. Master the six core syntax elements—$, ., [], *, .., ?()—and you can precisely extract data from any nested JSON. The JSONPath tool provides real-time queries with highlight positioning, while JMESPath and JSON Formatter complete your JSON data processing workflow.