liveBook · Manning

JSON path language

As explained in BDD in Action, Second Edition

A JSONPath expression is a query expression for selecting or evaluating values within JSON data.

JSONPath is a query language for selecting values within JSON responses. In API acceptance tests, it extracts only the data a scenario needs, reducing dependence on incidental response fields. section 13.0 section 13.4.2

Use JSONPath to retrieve:

  • Top-level values by name, with typed access such as getString() and getInt(). section 13.4.3
  • Nested values through dot notation, such as passport.number. section 13.4.3
  • Nested structures as Java domain objects with getObject(). section 13.4.3
  • Collections of matching values or objects with getList(). section 13.4.3

Extracting a registration number

After registering a frequent-flyer member through a REST API, retrieve only the returned identifier: response.jsonPath().getString("frequentFlyerNumber"). The test can then use that value without depending on the complete JSON structure. section 13.4.2

Listing 13.1 A sample JSON response
{
  "frequentFlyerNumber": 1000036,
  "firstName": "Tracy",
  "lastName": "Traveler",
  "email": "tracy@example.org",
  "flightHistory": [
    {
      "from": "London",
      "to": " Paris",
      "date": "2021-12-15"
    },
    {
      "from": "Paris",
      "to": " London",
      "date": "2021-12-21"
    }
  ],
  "passport": {
    "number": "12345678",
    "country": "United Kingdom"
  }
}

A sample response provides the nested passport object and flight-history array used by the JSONPath examples. section 13.4.3

How BDD in Action, Second Edition covers it

How deep it goes
Demonstrates scalar, nested, typed, object, and collection extraction but does not develop JSONPath grammar or implementation internals.
Examples
Uses frequent-flyer registration, passport lookup, and flight-history extraction as concrete query examples.
What it assumes
Assumes REST API calls and JSON response structures as the working context.
What you can do after
Apply it via Rest Assured to extract scalar values, nested objects, or collections from API responses.
How it connects
Connects JSON documents with REST APIs, Rest Assured, API acceptance testing, and BDD.

Also explained in

Compare how 3 books explain JSON path language →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.