Skip to main content
SnipKit

JSONPath Cheatsheet: 10 Expressions Every API Developer Should Know

SnipKit Team7 min read

You're staring at a 200-line API response, and you need one field buried three levels deep. JSONPath is the shortest path between you and that data. Most jsonpath tester tools drop you into an empty text box with no map — you're expected to already know the syntax. This article gives you 10 expressions that cover roughly 95% of real-world use, plus the 4 mistakes that eat 30 minutes of debugging time.

Before you start writing expressions, make sure your JSON is valid. Run it through SnipKit's JSON Validator first — even the best jsonpath tester returns nothing when the input itself is broken, leaving you chasing a syntax bug instead of a path bug.

The 10 Expressions (Cheatsheet)

Here is the table. Skim it once in your favorite jsonpath tester, then keep it open while you read the sections below.

ExpressionDoesExample
$Root of the document$ returns the entire JSON
$.foo.barNested property (dot notation)$.user.name
$['foo']Nested property (bracket notation)$['order-id'] — for keys with dashes or spaces
$.items[0]Array indexFirst item in items
$.items[-1]Last item (negative index)Most recent entry
$.items[*]Every item in an arrayAll items, flattened
$.items[?(@.status == 'paid')]Filter expressionPaid orders only
$..emailRecursive descent — finds every email anywhereGreat for deeply nested objects
$.items[0:3]Slice — first 3 itemsPagination preview
$.items[0,2,5]Union — pick specific indicesSampling non-sequential items

The 4 core operators

These four operators appear in almost every expression you will write.

  • $ — the root node. Every path starts here.
  • @ — the current item. You use this inside filter expressions: [?(@.price > 50)].
  • * — wildcard. $.users[*].name grabs every user's name.
  • .. — recursive descent. $..email finds email at any depth. It is the most useful operator and also the easiest to misuse. Place it carelessly and you pull far more data than intended.

3 Real-World Debugging Scenarios

Extract IDs from a paginated API list

You call a REST API and get back a paginated envelope. You need just the IDs to feed to a second request.

{
  "page": 1,
  "total": 48,
  "data": [
    { "id": "u_001", "name": "Alice", "role": "admin" },
    { "id": "u_002", "name": "Bob",   "role": "viewer" },
    { "id": "u_003", "name": "Carol", "role": "admin" }
  ]
}

Returns: ["u_001", "u_002", "u_003"]

The [*] iterates every item in data, and .id plucks the id field from each. Drop this JSON into the click-driven jsonpath tester at SnipKit's JSON Path Finder and click any id field — the expression appears at the top instantly.

Find every error in a nested response tree

Some APIs bury validation errors several levels deep. Recursive descent finds them all in one shot.

{
  "status": "error",
  "checkout": {
    "shipping": {
      "errors": [
        { "code": "E101", "message": "Address line 1 is required" }
      ]
    },
    "payment": {
      "errors": [
        { "code": "E202", "message": "Card expired" }
      ]
    }
  }
}

Expression: $..errors[*].message

Returns: ["Address line 1 is required", "Card expired"]

The ..errors descends through every level to find any key named errors, then [*].message pulls the message from each. Drop this JSON into SnipKit's JSON Path Finder and click a message field — the expression builds itself, no jsonpath tester guesswork required.

Filter orders by status and amount

You want only pending orders above a threshold — a filter expression with two conditions.

{
  "orders": [
    { "id": "o1", "status": "paid",    "total": 250 },
    { "id": "o2", "status": "pending", "total": 130 },
    { "id": "o3", "status": "pending", "total": 40  },
    { "id": "o4", "status": "failed",  "total": 90  }
  ]
}

Expression: $.orders[?(@.status == 'pending' && @.total > 100)]

Returns: the object for o2 only ({ "id": "o2", "status": "pending", "total": 130 }).

The @ refers to each order as the filter iterates. Once you have those objects, you can format the extracted data further. SnipKit's JSON Formatter helps you read the output visually before processing it.

4 Mistakes That Make Your Path Return Nothing

Case sensitivity

JSONPath is case-sensitive. $.Items and $.items are different keys. If your expression returns an empty array, check the exact casing of every key in the original JSON before assuming the path is wrong.

Quotes inside brackets

Most implementations expect single quotes inside bracket notation: $['user-id'], not $["user-id"]. Double quotes work in some libraries and fail silently in others. Default to single quotes and you sidestep the inconsistency entirely.

The forbidden .[ combination

Write $.items[0], not $.items.[0]. The extra dot before the bracket is invalid in every major implementation. The error message — when you get one — is rarely helpful, so this mistake wastes disproportionate time.

Hard-coded indices are brittle

$.orders[0] breaks the moment the API returns an empty array or reorders results. Use a filter instead: $.orders[?(@.status == 'pending')]. Filters survive schema changes and reorders — hard-coded indices do not.

How Implementations Differ — Quick Reference

LibraryLanguageNotes
jsonpath-plusJavaScriptFull spec, browser-friendly, active maintenance
jsonpath-ngPythonMost-used Python library, supports filters
json-pathJavaUsed in REST Assured and Spring integration tests
jqCLIDifferent syntax — solves similar jobs but is not JSONPath

Expressions usually port between these libraries without changes. The exception is filter syntax (?(...)) and recursive descent edge cases — test complex expressions in a jsonpath tester before shipping them to production.

FAQ

What's the difference between JSONPath and JSON Pointer?

JSON Pointer (RFC 6901) uses slash-separated paths like /user/name and always returns one value. JSONPath uses $-based expressions, supports wildcards and filters, and can return multiple values. Use JSON Pointer for precise single-value lookup; use JSONPath for querying.

Why is my JSONPath returning an empty array?

Check these four things in order: key casing mismatch, a stray dot before a bracket (.[), double quotes in a library that expects single, and invalid JSON input. Start with SnipKit's JSON Validator to rule out the last one fast, then run the expression through a jsonpath tester with a known-good payload to isolate the syntax issue.

Can I use JSONPath in Postman, Insomnia, or kubectl?

Yes. Postman has built-in JSONPath support in test scripts (pm.response.json()). Insomnia supports it via a plugin. kubectl accepts JSONPath with the -o jsonpath= flag — for example, kubectl get pods -o jsonpath='{.items[*].metadata.name}'. kubectl wraps the expression in curly braces, which is its own convention.

Conclusion

Master $, .., and [?(@...)] and you will handle 80% of what API work throws at you. The other 20% is slices, unions, and library-specific quirks — all in the cheatsheet table above.

Stop counting brackets. Open SnipKit's JSON Path Finder, paste your JSON, and click the node you want. The expression appears at the top immediately — it is the fastest jsonpath tester for click-driven path generation, with no typing and no guessing.

Related Articles