0% found this document useful (0 votes)
2 views

JSONPath_Examples_RestAssured

The document provides a comprehensive guide on using JSONPath in Rest Assured for querying JSON data in API tests. It includes various examples demonstrating how to extract single values, lists, nested values, and apply filters and conditions. The examples range from basic extraction to more complex queries involving aggregation and nested structures.

Uploaded by

sangee.prabha22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JSONPath_Examples_RestAssured

The document provides a comprehensive guide on using JSONPath in Rest Assured for querying JSON data in API tests. It includes various examples demonstrating how to extract single values, lists, nested values, and apply filters and conditions. The examples range from basic extraction to more complex queries involving aggregation and nested structures.

Uploaded by

sangee.prabha22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Examples based on JSONPath in Rest Assured

JSONPath is a syntax for querying JSON data, similar to XPath for XML. In the context of Rest
Assured, JSONPath is used to extract specific pieces of information from a JSON response. It
allows you to traverse the JSON structure, filter data, and retrieve the values you need for your
API tests.

JSON Path is a powerful way to query and extract specific data from JSON documents. In Rest
Assured, JSON Path is used to navigate and extract data from JSON responses efficiently. JSON
Path expressions are akin to XPath expressions used in XML documents.
Rest Assured provides a convenient way to apply JSONPath expressions to a JSON response. It
can be used to assert values, extract data for further use, or perform various other checks on
the response.
JSONPath Syntax Basics
• Root: $ refers to the root of the JSON.
• Dot Notation: Accesses child elements, e.g., $.store.book.
• Bracket Notation: Alternative to dot notation, useful for accessing keys with special
characters or spaces, e.g., $['store']['book'].
• Wildcards: * can be used to match any element, e.g., $..author (all authors).
• Array Indexing: Access specific elements of an array, e.g., $.store.book[0].
• Filters: [?(@.price < 10)] filters items based on a condition.

Example 1: Extracting a Single Value


Scenario: Mike wants to extract the firstName from a JSON response.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class SimpleJsonPathExample1 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users/1");
String firstName = from(response.asString()).getString("firstName");
System.out.println("First Name: " + firstName);
}
}
Example 2: Extracting a List of Values
Scenario: Mahii wants to extract all emails from a JSON response containing a list of users.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;
import java.util.List;

public class SimpleJsonPathExample2 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
List<String> emails = from(response.asString()).getList("email");
System.out.println("Emails: " + emails);
}
}
Example 3: Extracting a Nested Value
Scenario: Sweta wants to extract the city from a nested address object in the JSON response.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class SimpleJsonPathExample3 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users/1");
String city = from(response.asString()).getString("address.city");
System.out.println("City: " + city);
}
}
Example 4: Extracting Multiple Values Using a Loop
Scenario: Mike wants to extract all phone numbers from a list of users.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;
import java.util.List;

public class SimpleJsonPathExample4 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
List<String> phoneNumbers = from(response.asString()).getList("phone");
phoneNumbers.forEach(phone -> System.out.println("Phone: " + phone));
}
}
Example 5: Extracting a Boolean Value
Scenario: Mahii wants to check if the isActive status of a user is true.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class SimpleJsonPathExample5 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users/1");
boolean isActive = from(response.asString()).getBoolean("isActive");
System.out.println("Is Active: " + isActive);
}
}
Example 6: Filtering Data
Scenario: Sweta wants to extract firstName of users whose age is greater than 30.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;
import java.util.List;

public class ModerateJsonPathExample1 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
List<String> firstNames = from(response.asString()).getList("findAll { it.age > 30
}.firstName");
System.out.println("First Names: " + firstNames);
}
}
Example 7: Extracting Data Based on Conditions
Scenario: Mike wants to get the email of the user whose username is "mike123".
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class ModerateJsonPathExample2 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
String email = from(response.asString()).getString("find { it.username == 'mike123'
}.email");
System.out.println("Email: " + email);
}
}
Example 8: Extracting Part of a Nested Object
Scenario: Mahii wants to extract the zipcode from the address of a user where the city is "New
York".
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class ModerateJsonPathExample3 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
String zipcode = from(response.asString()).getString("find { it.address.city == 'New York'
}.address.zipcode");
System.out.println("Zipcode: " + zipcode);
}
}
Example 9: Extracting Nested Arrays
Scenario: Sweta wants to extract the titles of all books owned by the user "sweta123".
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;
import java.util.List;
public class ModerateJsonPathExample4 {
public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
List<String> titles = from(response.asString()).getList("find { it.username == 'sweta123'
}.books.title");
System.out.println("Book Titles: " + titles);
}
}
Example 10: Extracting Data with Specific Index
Scenario: Mike wants to get the firstName of the second user in the JSON array.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class ModerateJsonPathExample5 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
String firstName = from(response.asString()).getString("[1].firstName");
System.out.println("First Name of Second User: " + firstName);
}
}
Example 11: Using Regular Expressions
Scenario: Mahii wants to extract the email of users whose email domain is example.com.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;
import java.util.List;
public class HardJsonPathExample1 {
public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
List<String> emails = from(response.asString()).getList("findAll { it.email ==~
/.*@example.com/ }.email");
System.out.println("Emails: " + emails);
}
}
Example 12: Extracting Data from Deeply Nested Structures
Scenario: Sweta wants to get the price of a specific product from a deeply nested orders
structure.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class HardJsonPathExample2 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/orders");
double price = from(response.asString()).getDouble("orders.find { it.id == 123 }.items.find {
it.name == 'ProductA' }.price");
System.out.println("Price of ProductA: " + price);
}
}
Example 13: Combining Multiple Conditions
Scenario: Mike wants to extract the username of users who are active and live in "California".
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;
import java.util.List;

public class HardJsonPathExample3 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
List<String> usernames = from(response.asString()).getList("findAll { it.isActive &&
it.address.state == 'California' }.username");
System.out.println("Usernames: " + usernames);
}
}
Example 14: Using Aggregation Functions
Scenario: Mahii wants to find the maximum age of users in the list.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;

public class HardJsonPathExample4 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/users");
int maxAge = from(response.asString()).getInt("max { it.age }");
System.out.println("Maximum Age: " + maxAge);
}
}
Example 15: Extracting Data with Complex Path
Scenario: Sweta wants to extract all product names from orders where the totalAmount is
greater than 1000.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static io.restassured.path.json.JsonPath.from;
import java.util.List;

public class HardJsonPathExample5 {


public static void main(String[] args) {
Response response = RestAssured.get("https://api.example.com/orders");
List<String> productNames = from(response.asString()).getList("findAll { it.totalAmount >
1000 }.items.name.flatten()");
System.out.println("Product Names: " + productNames);
}
}
These examples cover different complexities of using JSON Path in Java Rest Assured,
showcasing how to query JSON data ranging from simple value extraction to complex filtering
and aggregation.

You might also like