Skip to content
data2026-07-252 min read

What Is JSONPath

JSONPath is a query language for extracting information from JSON data structures, similar to XPath for XML. It uses path expressions to locate and extract specific data from JSON.

Basic Syntax

| Expression | Description | Example | |------------|-------------|---------| | $ | Root node | $ | | . | Child node | $.name | | [] | Child node/filter | $['name'] | | * | Wildcard | $.* | | .. | Recursive search | $..name | | @ | Current node | $.users[?(@.age>25)] |

Why You Need JSONPath

  1. API data extraction: Precisely extract needed fields from complex API responses
  2. Data transformation: Extract and restructure nested JSON data
  3. Log analysis: Filter specific info from structured logs
  4. Config management: Read specific values from multi-level config files
  5. Test assertions: Verify specific field values in API testing
  6. Data migration: Map fields between different data structures

JSONPath Expressions Explained

Sample JSON Data

{
  "store": {
    "books": [
      { "title": "JavaScript: The Definitive Guide", "price": 99, "category": "Programming" },
      { "title": "CSS: The Definitive Guide", "price": 79, "category": "Frontend" },
      { "title": "HTTP: The Definitive Guide", "price": 89, "category": "Networking" }
    ],
    "bicycle": { "color": "red", "price": 299 }
  }
}

Common Expressions

| JSONPath | Result | |----------|--------| | $.store.books[*].title | All book titles | | $.store.books[0] | First book | | $.store.books[-1] | Last book | | $.store.books[?(@.price<85)] | Books under $85 | | $.store.books[?(@.category=='Frontend')].title | Frontend book titles | | $.store..price | All prices | | $.store.books.length() | Number of books |

How to Use an Online Tool

Using DevToolkit Pro's JSONPath Query Tool:

  1. Paste JSON data
  2. Enter JSONPath expression
  3. Real-time query results
  4. Syntax highlighting and error detection
  5. Built-in expression templates

JSONPath in Code

JavaScript

// Simple JSONPath implementation
function jsonPath(obj, path) {
  const keys = path.replace(/\$/,'').replace(/\[(\d+)\]/g, '.$1').split('.').filter(Boolean);
  return keys.reduce((current, key) => current?.[key], obj);
}

// Usage
const data = { store: { books: [{ title: 'JS Guide', price: 99 }] } };
const title = jsonPath(data, '$.store.books[0].title'); // 'JS Guide'

Python

from jsonpath_ng import parse

expression = parse('$.store.books[?(@.price<90)]')
matches = expression.find(data)
for match in matches:
    print(match.value)

JSONPath vs JSON Pointer

| Feature | JSONPath | JSON Pointer | |---------|----------|-------------| | Syntax | XPath-like | URL path-like | | Filtering | ✅ Supports conditions | ❌ No | | Wildcards | ✅ Supported | ❌ No | | Recursive search | ✅ .. syntax | ❌ No | | Standard | Community standard | RFC 6901 | | Use case | Complex queries | Simple lookup |

FAQ

Is There a Standard for JSONPath?

JSONPath has no official RFC standard yet, but there's a widely accepted community standard. Different language implementations may have minor differences, but core syntax is consistent.

How to Handle Chinese Characters in JSONPath?

JSONPath expressions support Unicode natively — you can use Chinese key names directly: $.data['中文键名']. However, English key names are recommended to avoid encoding issues.

How Is JSONPath Query Performance?

Performance depends on expression complexity and data structure. Simple path queries ($.data[0].name) are fast. Recursive searches (..) may be slower on deeply nested data. Cache results at the application layer for repeated queries.


This article is brought to you by DevToolkit Pro. More developer tools at the homepage.


ad