- Basic JSONPath syntax
- Common operators and filters
- Practical Python examples with
jsonpath-ng
1. Basic JSONPath Syntax
Consider the following sample JSON document:$: the root object.or[]: child access*: wildcard match (all elements)..: recursive descent
$.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.
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:
Filter operators include:
<,<=,>,>=,==,!=- Boolean AND/OR (
&&,||)
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:
-
Use the following script:
Expected output: