JSONPath: How to Query and Filter JSON Data Effectively

jsonpathjsonquerydata-filtering

JSONPath is a query language for JSON, similar to XPath for XML. It lets you extract specific values from complex JSON structures quickly and efficiently.

Basic JSONPath Syntax

Given this JSON:

{

"store": {

"books": [

{ "title": "JSON Guide", "price": 29.99 },

{ "title": "API Design", "price": 39.99 },

{ "title": "Web Security", "price": 19.99 }

],

"location": "New York"

}

}

Here are common JSONPath expressions:

  • $.store.books[0].title — First book's title
  • $.store.books[*].price — All book prices
  • $.store.books[?(@.price < 30)] — Books under $30
  • $.store.books[?(@.price > 20)].title — Titles of books over $20
  • $.store.. — Everything under store
  • Essential Operators

  • $ — Root element
  • . — Child element
  • [] — Array index or filter
  • *** — Wildcard (all elements)
  • .. — Recursive descent
  • ?() — Filter expression
  • @ — Current element (in filters)
  • Practical Use Cases

  • API Response Filtering — Extract only the fields you need
  • Data Validation — Check that specific paths exist
  • Configuration Extraction — Pull settings from nested config files
  • Testing** — Assert specific values in JSON responses
  • JSONPath in JavaScript

    // Using JSONPath Plus library

    const jp = require('jsonpath-plus');

    const result = jp.JSONPath({path: '$.store.books[?(@.price < 30)]', json: data});

    Tips for Complex Queries

  • Start simple — query one level at a time
  • Use the recursive descent .. to find deeply nested values
  • Combine filters with && (and) and ||` (or)
  • Test your queries with formatted JSON first
  • Use our JSON Formatter to format your data before writing JSONPath queries, or try the JSON Viewer for interactive exploration.

    Related Tools