0

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
      ] 
    }
   }
}
3
  • that's because "singleAccomViewData": null in your JSON Commented Aug 20, 2019 at 21:26
  • no, even if i try for others, there also i get the empty list. say for *.singleAccomSearch Commented Aug 20, 2019 at 21:30
  • @SeanBright still it gives me the empty list Commented Aug 20, 2019 at 21:43

1 Answer 1

2

There are a few reasons why the above is not working:

  1. 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.

  2. Using * for the root of your JsonPath is not valid, it should be $.

  3. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.