Reputation: 61
I am trying to fetch the values from json file with the help of jayway JsonPath. But everytime it returns empty list.
I am trying to use the json path as *.singleAccomViewData
import java.util.*;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
public class JSONMapper {
public static void main(String[] args) throws Exception {
DocumentContext jsonContext = JsonPath.parse("D:\\Docs\\search.json");
List<String> accom = JsonPath.read("D:\\Docs\\search.json", "*.singleAccomViewData");
System.out.println("accom value: " + accom);
}
}
Below is my JSON File:
{
"searchResult": {
"singleAccomViewData": null,
"singleAccomSearch": false,
"durationSelection": {
"defaultDisplay": [
6,
7,
8,
9,
10
]
}
}
}
Upvotes: 0
Views: 1600
Reputation: 405
There are a few reasons why the above is not working:
If you are calling JsonPath.read(String, String)
then the first String is expected to be the Json itself, not a path to a Json file.
Using *
for the root of your JsonPath is not valid, it should be $
.
There is another object above what you are looking for: searchResult
.
Your final JsonPath should be $.searchResult.singleAccomViewData
. Fix the above issues and it should work as expected.
Upvotes: 2