JSON Path Test - Free Course
Lesson
Introduction to JSON PATH
JSON (JavaScript Object Notation) is a lightweight data-interchange format that’s easy to read, write, parse, and generate. When working with complex JSON—nested objects, arrays, and mixed data types—you need a concise way to extract specific values. Enter JSONPath, a query language inspired by XPath for XML. JSONPath provides a simple, declarative syntax to traverse and filter JSON structures.
In this guide, we’ll explore:
- Basic JSONPath syntax
- Common operators and filters
- Practical Python examples with
jsonpath-ng
1. Basic JSONPath Syntax
Consider the following sample JSON document:
{
"store": {
"book": [
{ "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 },
{ "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 },
{ "category": "fiction", "author": "Herman Melville","title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 },
{ "category": "fiction", "author": "J. R. R. Tolkien","title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 }
],
"bicycle": { "color": "red", "price": 19.95 }
}
}
Key JSONPath tokens:
$
: the root object.
or[]
: child access*
: wildcard match (all elements)..
: recursive descent
Examples:
$.store.book
→ Returns the array of all books.$.store.book[0].author
→"Nigel Rees"
$..price
→ All price values in the document.$.store.book[?(@.price < 10)]
→ Books with a price less than 10.
Note
JSONPath expressions are case-sensitive. Always match the exact key names when querying.
2. Common Operators and Filters
JSONPath offers a variety of operators for powerful data selection. Below is a quick reference table:
Operator | Description | Example |
---|---|---|
* | Wildcard: matches all child members | $.store.* |
.. | Recursive descent through all object levels | $..author |
[index] | Array index access | $.store.book[2] |
[start:end] | Slicing an array | $.store.book[0:2] |
[?(@.key <op> value)] | Filter expression | $.store.book[?(@.price > 10)] |
Filter operators include:
<
,<=
,>
,>=
,==
,!=
- Boolean AND/OR (
&&
,||
)
Warning
Ensure your filter syntax is valid. A misplaced parenthesis or comparison operator can lead to no results or errors.
3. Python Examples with jsonpath-ng
Follow these steps to query JSON with Python:
Install the library:
pip install jsonpath-ng
Use the following script:
import json from jsonpath_ng import parse data = { "store": { "book": [ {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95}, {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99}, {"category": "fiction", "author": "Herman Melville","title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99}, {"category": "fiction", "author": "J. R. R. Tolkien","title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99} ], "bicycle": {"color": "red", "price": 19.95} } } # Parse and evaluate a JSONPath expression jsonpath_expr = parse('$.store.book[?(@.price < 10)].title') matches = jsonpath_expr.find(data) # Extract and print titles titles = [match.value for match in matches] print(titles)
Expected output:
['Sayings of the Century', 'Moby Dick']
Further Reading and References
By mastering JSONPath, you can dramatically simplify data extraction from nested JSON structures—making your scripts cleaner and more maintainable. Give it a try in your next project!
Watch Video
Watch video content
Practice Lab
Practice lab