I have a below JSON string from the below i want to find/search criteria in JSON String.
1). To find the Number of keys present. 2). To get the values of given key(
Firstly, add json-path dependency in pom
com.jayway.jsonpath
json-path
2.2.0
Create a utility in Base Class that can be reused :
public static String getValueFromJsonPath(String apiResponse, String jsonPath) {
return JsonPath.read(apiResponse, jsonPath);
}
One example of using this method :
getValueFromJsonPath(apiResponse, "$.store.book[0].category");
NOTE : Create overloaded method with different return types depending upon the expected result (like List for fetching a list from json)