🧪Extracting Values from Local JSON Using JsonPath in Rest Assured

Author: Dinesh Y S
🔍Introduction
When working with API responses, validating JSON data is a common task. But what if you want to practice or test your logic using a local JSON file instead of hitting a live API?
In this post, I’ll show you how to:
Read a local JSON file in Java
Use JsonPath to extract values
Filter JSON arrays using Groovy-style expressions like find and it
📁Sample JSON File
Save this as users.json in your src/test/resources/testData/ folder:
[
{ "id": 1, "firstName": "John", "status": "active" },
{ "id": 2, "firstName": "John", "status": "inactive" },
{ "id": 3, "firstName": "Dinesh", "status": "pending" }
]
🛠️Utility to Read JSON File
import java.nio.file.Files;
import java.nio.file.Paths;
public class JsonUtils {
/**
* Reads a JSON from the given file path and returns it as a String
* @param filePath path to the JSON file
* @return JSON content as String
* @throws Exception if file reading fails
*/
public static String readJson(String filePath) throws Exception{
return new String(Files.readAllBytes(Paths.get(filePath)));
}
}
🧪Test Class Using JsonPath
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.annotations.Test;
public class LocalJsonTest {
@Test
public void testGetStatusByIdAndfirstName() throws Exception {
String json = JsonUtil.readJson("src/test/resources/testData/users.json");
JsonPath jsonPath = new JsonPath(json);
// Extract sttaus using Groovy-style filter
String status = jsonPath.getString("find{ it.id == 3 &7 it.firstname == 'Dinesh' }.status");
Assert.assertEquals(status, "pending", "Status should match for given ID and firstName");
}
}
🧠Why find and it?
find {} is a Groovy method that returns the first matching element
it refers to the current object in the iteration.
So this line:
jsonPath.getString("find { it.id == 3 && it.firstName == 'Dinesh' }.status");
….means: “ Find the first object where id is 3 and firstName is Dinesh, and returns its status.”
✅Conclusion
This is great way to practice JSONPath filtering without relying on external APIs. You can extend this by:
Using findAll to get multiple matches
Adding DataProvider for parameterized tests
Validating nested JSON structures
Subscribe to my newsletter
Read articles from Dinesh Y S directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Dinesh Y S
Dinesh Y S
Automation Engineer | Java + Selenium | Appium + Java | RestAssured | Sharing real-world automation tips