hank-builds.com

JSONPath Syntax Guide - Query & Filter JSON Data

What is JSONPath?

JSONPath is a query language for JSON, similar to XPath for XML. It lets you navigate and extract data from JSON structures using path expressions. JSONPath expressions always start with $ representing the root element.

Basic Syntax

$Root object or array
$.storeChild property "store"
$.store.bookNested property access
$['store']['book']Bracket notation (equivalent)
$.store.book[0]First element of array
$.store.book[-1]Last element of array

Wildcards & Recursive Descent

$.store.*All direct children of store
$.store.book[*]All items in the book array
$..nameAll "name" properties at any depth
$..*Every value in the entire JSON

The recursive descent operator .. searches the entire tree. Use it to find all occurrences of a key regardless of nesting depth.

Array Slicing

$[0:3]Items at indices 0, 1, 2
$[1:5]Items at indices 1 through 4
$[::2]Every other item (step 2)
$[-3:]Last 3 items
$[0,2,4]Items at specific indices

Array slices use the syntax [start:end:step], similar to Python. The end index is exclusive.

Filter Expressions

Filter expressions select items that match a condition. The @ symbol refers to the current item being evaluated.

$[?(@.price < 10)]Items where price is less than 10
$[?(@.isbn)]Items that have an isbn property
$[?(@ === "hello")]Items equal to the string "hello"
$[?(@.price > 5 && @.category === "fiction")]Combined conditions

Searching by Value

Combine recursive descent with filters to search an entire document for values:

$..[?(@ === 42)]Find all values equal to 42
$..[?(@.name === "John")]Find objects where name is "John"
$..[?(@ *= "error")]Find strings containing "error"

The *= operator performs a substring match, making it useful for searching within string values.