Skip to main content
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:
  1. Basic JSONPath syntax
  2. Common operators and filters
  3. Practical Python examples with jsonpath-ng

1. Basic JSONPath Syntax

Consider the following sample JSON document:
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.
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:
  1. Install the library:
  2. Use the following script:
    Expected output:

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

Practice Lab