Last updated
Basic Dot Notation Access
Given the following JSON representing a bookstore, use JSONPath to extract specific fields. Start with simple dot notation to access a top-level property:
{
"store": {
"name": "City Books",
"books": [
{ "title": "Clean Code", "author": "Robert Martin", "price": 35.99, "category": "programming" },
{ "title": "The Pragmatic Programmer", "author": "David Thomas", "price": 42.00, "category": "programming" },
{ "title": "Dune", "author": "Frank Herbert", "price": 12.99, "category": "fiction" },
{ "title": "Foundation", "author": "Isaac Asimov", "price": 9.99, "category": "fiction" }
]
}
}
JSONPath expression to get the store name:
$.store.name
Result:
["City Books"]
Accessing All Array Elements
Use the wildcard * to select all elements in an array:
$.store.books[*].title
Result:
["Clean Code", "The Pragmatic Programmer", "Dune", "Foundation"]
This is equivalent to iterating over the entire books array and extracting the title from each element.
Array Index Access
Access a specific element by its zero-based index:
$.store.books[0].title
Result:
["Clean Code"]
Access the last element using a negative index:
$.store.books[-1].title
Result:
["Foundation"]
Array Slicing
Extract a range of array elements using slice notation [start:end]:
$.store.books[0:2].title
Result:
["Clean Code", "The Pragmatic Programmer"]
Extract every other book using step notation:
$.store.books[::2].title
Result:
["Clean Code", "Dune"]
Extract the last two books:
$.store.books[-2:].title
Result:
["Dune", "Foundation"]
Filter Expressions — Price Comparison
Use filter expressions with [?()] to select elements matching a condition. Find all books priced under $15:
$.store.books[?(@.price < 15)]
Result:
[
{ "title": "Dune", "author": "Frank Herbert", "price": 12.99, "category": "fiction" },
{ "title": "Foundation", "author": "Isaac Asimov", "price": 9.99, "category": "fiction" }
]
The @ symbol refers to the current element being evaluated in the filter.
Filter Expressions — String Equality
Filter books by category using string equality:
$.store.books[?(@.category == "programming")].title
Result:
["Clean Code", "The Pragmatic Programmer"]
Combine multiple conditions with logical AND:
$.store.books[?(@.category == "fiction" && @.price < 11)].title
Result:
["Foundation"]
Recursive Descent Operator
The .. operator searches all levels of nesting. Given a deeply nested JSON:
{
"company": {
"name": "Acme Corp",
"departments": [
{
"name": "Engineering",
"teams": [
{ "name": "Backend", "size": 8 },
{ "name": "Frontend", "size": 5 }
]
},
{
"name": "Marketing",
"teams": [
{ "name": "Digital", "size": 4 }
]
}
]
}
}
Find all name properties at any depth:
$..name
Result:
["Acme Corp", "Engineering", "Backend", "Frontend", "Marketing", "Digital"]
Extracting Nested API Response Fields
A typical REST API response with pagination:
{
"status": "ok",
"data": {
"users": [
{ "id": 1, "username": "alice", "active": true, "plan": "pro" },
{ "id": 2, "username": "bob", "active": false, "plan": "free" },
{ "id": 3, "username": "carol", "active": true, "plan": "pro" }
],
"pagination": { "page": 1, "total": 3 }
}
}
Extract only active users' usernames:
$.data.users[?(@.active == true)].username
Result:
["alice", "carol"]
Extract all pro plan users:
$.data.users[?(@.plan == "pro")].id
Result:
[1, 3]
Multiple Field Selection
Select multiple specific fields from array elements using bracket notation:
$.store.books[*]['title','price']
Result:
[
{ "title": "Clean Code", "price": 35.99 },
{ "title": "The Pragmatic Programmer", "price": 42.00 },
{ "title": "Dune", "price": 12.99 },
{ "title": "Foundation", "price": 9.99 }
]
Using JSONPath in API Test Assertions
When writing API tests in Postman or REST Assured, JSONPath expressions form the basis of response assertions. For a login API response:
{
"token": "eyJhbGciOiJIUzI1NiJ9...",
"user": {
"id": 42,
"email": "user@example.com",
"roles": ["user", "moderator"]
},
"expiresIn": 3600
}
- Assert token exists:
$.token— should not be null - Assert user ID:
$.user.id— should equal 42 - Assert role included:
$.user.roles[?(@ == "moderator")]— should return one result - Assert expiry:
$.expiresIn— should equal 3600
Test each expression in the JSONPath Tester before adding it to your test suite to confirm it matches the expected value.
Bracket Notation vs Dot Notation
Both notations are equivalent for most cases. Bracket notation is required when property names contain spaces or special characters:
// Dot notation
$.store.books[0].title
// Bracket notation (equivalent)
$['store']['books'][0]['title']
// Bracket notation required for special characters
$['store']['book-list'][0]['full-title']
The tester shows both forms and highlights which is required for your specific JSON structure.