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
      ] 
    }
   }
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
shashank sinha
  • 61
  • 2
  • 12

1 Answers1

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.

wearebob
  • 376
  • 2
  • 15