← Back to Blog2025-12-2610 min read

JSONPath in practice: querying JSON without writing code

A hands-on guide to JSONPath: selecting nested fields, filtering arrays, and debugging “why is this empty?” with practical examples.

By RawTools Teamjsonpath examples

What JSONPath is good for

JSONPath is a query language for JSON. It’s useful when you have a large JSON document and you want one slice of it: all IDs, the first matching object, a nested field across items, or a filtered list.

It’s not a replacement for a data pipeline, but it’s a fast way to answer “what’s in here?” without writing code.

Start with a mental model

  • $ is the root.
  • Dot notation selects object keys: $.user.email
  • Bracket notation selects keys or indices: $['user']['email'], $.items[0]
  • Wildcards select “all”: $.items[*].id

If you want to test expressions quickly, use JSON Query. If your JSON isn’t readable yet, format it first with JSON Formatter.

A small example JSON to follow along

{
  "order": {
    "id": "ord_1001",
    "total": 129.5,
    "currency": "USD",
    "customer": { "id": "cus_55", "email": "a@example.com" },
    "items": [
      { "sku": "TEE-BLK-M", "qty": 1, "price": 39.5, "tags": ["apparel"] },
      { "sku": "CAP-NVY", "qty": 2, "price": 45.0, "tags": ["accessory", "gift"] }
    ]
  }
}

Useful JSONPath patterns you’ll reuse

Select a single nested field

$.order.customer.email

Select all values from an array

$.order.items[*].sku

Filter an array

Get items where qty is greater than 1:

$.order.items[?(@.qty > 1)]

Pick a field from filtered results

Get SKUs where qty is greater than 1:

$.order.items[?(@.qty > 1)].sku

Find items containing a tag

$.order.items[?(@.tags && @.tags.indexOf('gift') > -1)].sku

Why does my query return an empty result?

Empty results usually come from one of these:

  • Wrong root: you’re querying $.items but the document has $.order.items.
  • Key mismatch: customerEmail vs customer.email.
  • Unexpected types: you’re filtering with numeric comparisons, but the field is a string (common after CSV imports).
  • Null vs missing: some items don’t have the field at all.

When the document is large, it can help to pick the path visually first. Use JSON Mapper to discover the exact key names and nesting, then translate that into a JSONPath expression.

Two practical workflows

1) Debugging API responses

  1. Format the response (JSON Formatter).
  2. Query the critical fields (status, totals, IDs) (JSON Query).
  3. If you need to compare responses, diff them (JSON Diff).

2) Cleaning data before transforming it

If the JSON came from a spreadsheet or CSV, you often start with strings everywhere. Convert with CSV to JSON or Excel to JSON, then validate or query.

FAQs

Is JSONPath standardized?

There are common conventions, but implementations differ slightly. If an expression behaves unexpectedly, test a simpler version and build up.

How do I select a key that contains special characters?

Use bracket notation: $['weird-key'] instead of dot notation.

Can I query multiple fields at once?

Many implementations support it, but results can vary. A reliable approach is to extract one field at a time, or query whole objects and then inspect.

Why does my filter fail even though the value is “10”?

Because it may be a string, not a number. Coerce in your pipeline, or adjust your comparisons. If you want to enforce types, validate with JSON Schema Validator.

How do I work with escaped JSON strings?

Unescape them first with JSON Escape/Unescape, then format and query.

What’s the fastest way to discover paths in unfamiliar JSON?

Use JSON Mapper to browse the structure, then switch to JSON Query when you’re ready to extract specific fields.

Can JSONPath replace writing code for transformations?

It’s great for selection and filtering. For reshaping data into a new structure, tools like JSON Mapper (or a real transform step) are better.

📑 Quick Info

📅
Published
2025-12-26
⏱️
Reading Time
10 min
✍️
Author
RawTools Team