How to Query JSON with JSONPath

A practical introduction to JSONPath: root, child and wildcard selectors, recursive descent, array slices and filter expressions, with examples.

JSONPath lets you pull values out of a JSON document with a short expression, in the same way XPath does for XML. This guide covers the syntax you need for most real queries.

The basics

Every expression starts with $, the root of the document. From there, .name or ['name'] selects a field and [0] selects an array item by position. So $.store.book[0].title walks from the root to the title of the first book.

Use the bracket form for keys that contain spaces or dots, such as $['first name'].

Wildcards and recursive descent

[*] or .* matches every child at one level, so $.store.book[*].title returns the title of every book. The double dot searches at any depth: $..title returns every title anywhere in the document, whatever the nesting.

Slices and filters

Array slices work like in Python: [0:2] returns the first two items and [-1:] the last one. Filter expressions select items by a condition, where @ stands for the current item, for example $..book[?(@.price < 10)] returns the books cheaper than 10.

Filters can combine conditions with && and ||, and check that a field exists with [?(@.isbn)].

Reading the result

A JSONPath query always returns a list of matches, even when there is only one, and an empty list when nothing matches. Implementations differ in the less common features, so test the expression against your real data before you rely on it in code.

Related tools

All guides