How to Query JSON with JSONPath

How to Query JSON with JSONPath

JSONPath is a compact query language for pulling specific values out of a JSON document, the way XPath does for XML. Instead of looping through nested objects in code, you write one expression like $.store.book[*].title and get back exactly the values you want. It is perfect for testing API responses and digging into deep, messy payloads.

JSONPath syntax in one pass

Every expression starts at the root and walks down into the structure. These are the pieces you will use most:

  • $ is the root of the document. Every query begins here.
  • .name selects a child by key, for example $.store.
  • .. is recursive descent: it searches every level below the current point for a match. $..price finds every price anywhere in the document, no matter how deep.
  • [0] selects an array element by index. ['key'] is bracket notation for a child, useful when a key has spaces or dots.
  • * is the wildcard: it matches all elements of an array or all values of an object. $.store.book[*] is every book.
  • [start:end] is an array slice, end-exclusive, like Python. [0:2] returns the first two items.
  • [?(@.price < 10)] is a filter expression. @ refers to the current element, so this keeps only items whose price is under 10.

Worked examples

Here is a small sample to query against:

{
  "store": {
    "book": [
      { "title": "JSON Basics", "price": 8, "active": true },
      { "title": "Advanced APIs", "price": 24, "active": false },
      { "title": "Query Languages", "price": 6, "active": true }
    ]
  },
  "items": [
    { "id": 1, "active": true },
    { "id": 2, "active": false }
  ]
}

Get every book title with a wildcard:

$.store.book[*].title

Returns ["JSON Basics", "Advanced APIs", "Query Languages"].

Find every price in the whole document with recursive descent:

$..price

Returns [8, 24, 6]. Notice you never named store or book. The .. walks the entire tree for you.

Keep only active items with a filter:

$.items[?(@.active==true)]

Returns the object with id: 1. Swap in $.store.book[?(@.price < 10)].title to get the titles of the cheap books, which gives ["JSON Basics", "Query Languages"].

Test JSONPath in your browser

The fastest way to learn the syntax is to run expressions against your own data and watch the matches highlight.

  1. Open the JSONPath Tester and paste your JSON into the input.
  2. Type an expression in the query box, starting with $.
  3. Read the matched values in the results panel.
  4. Tweak the path, add a * wildcard or a [?()] filter, and rerun until it returns exactly what you need.

Everything runs client-side, so your JSON never leaves your device. Paste a real API response, confirm the path you plan to use in code, and you will catch mistakes before they ship. When an expression returns nothing, the JSONPath Tester makes it obvious so you can adjust the key or filter.

Write the path once, test it live, and stop hand-rolling loops just to read one value.

← All posts