Fundamentals 6 min read

Core Concepts, Syntax, and Applications of JSONPath

This article introduces JSONPath as a query language for JSON, explains its core concepts and basic syntax with examples, demonstrates various practical use cases such as data extraction, API testing, and shows tool support across Python, Java, and JavaScript.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Core Concepts, Syntax, and Applications of JSONPath

1. Core Concept JSONPath is a query language for extracting specific fields or values from JSON data, with an expression syntax similar to XPath, supporting multiple operators and functions for fast data location.

2. Basic Syntax

2.1 Extract a single field Syntax: $.store.book[0].title

Example JSON:

{
  "store": {
    "book": [
      {"title": "Sword of Honour", "price": 12.99},
      {"title": "Moby Dick", "price": 8.99}
    ]
  }
}

Result: $.store.book[0].title extracts the title "Sword of Honour".

2.2 Extract all elements in an array Syntax: $.store.book[*].title

Result: extracts all book titles.

2.3 Filter condition Syntax: $.store.book[?(@.price > 10)].title

Result: extracts titles of books with price greater than 10.

2.4 Recursive query Syntax: $..name

Result: extracts all "name" fields at any depth.

3. Application Scenarios

3.1 Extract specific fields Example: extracting a student's name and math score.

{
  "student": {
    "name": "John Doe",
    "scores": {"math": 85, "english": 90}
  }
}

Expressions: $.student.name and $.student.scores.math .

3.2 Extract array elements Extract all math scores from multiple students.

{
  "students": [
    {"name": "John Doe", "scores": {"math": 85}},
    {"name": "Jane Smith", "scores": {"math": 92}}
  ]
}

Expression: $.students[*].scores.math .

3.3 Extract specific items from nested arrays Extract product names with price > 50.

{
  "orders": [
    {"items": [{"name": "item1", "price": 45}, {"name": "item2", "price": 55}]},
    {"items": [{"name": "item3", "price": 60}]}
  ]
}

Expression: $.orders[*].items[?(@.price > 50)].name .

3.4 Extract selected attributes from nested objects Extract book titles and publication years.

{
  "library": {
    "books": [
      {"title": "Book A", "year": 2020},
      {"title": "Book B", "year": 2019}
    ]
  }
}

Expression: $.library.books[*]['title', 'year'] .

4. JSONPath in API Testing

4.1 Extract data from API responses, e.g., token from login response.

{
  "status": "success",
  "data": {"token": "abc123"}
}

Expression: $.data.token .

4.2 Validate response content, e.g., verify username.

{
  "profile": {"name": "admin", "email": "[email protected]"}
}

Expression: $.profile.name .

5. Tool Support

Python: import jsonpath result = jsonpath.jsonpath(data, "$.store.book[*].title") print(result) # ['Sword of Honour']

Java: import com.jayway.jsonpath.JsonPath; Object result = JsonPath.read(json, "$.store.book[*].title"); System.out.println(result);

JavaScript: const jsonpath = require('jsonpath-plus'); const result = jsonpath({ path: '$.store.book[*].title', json: data }); console.log(result); // ['Sword of Honour']

6. Summary JSONPath is a powerful tool for quickly extracting required information from complex JSON data, widely used in automated API testing for data extraction and validation, and mastering its syntax and scenarios can greatly improve efficiency and accuracy in handling JSON.

JavaJavaScriptPythonJSONJsonPathData ExtractionAPI testing
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.