1

For the below sample JSON when I tried to print the root node using $: It is printing the entire JSON as a result:

{
  "ABC,123": {
    "p1": 100,
    "s1": 66,
    "o1": 10,
     }
}

I need to get ABC,123 using a JSON path.

I tried as $ it's printing full JSON.

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Is this an escaping problem? Try `System.out.println(jsonPath.get("'ABC,123'").toString());` Or do you actually want to get the string `ABC,123` as a result? – wp78de Aug 10 '21 at 22:14
  • Want to get abc,123 as result....this is dynamic value which keeps changing – user16634373 Aug 11 '21 at 08:10

1 Answers1

0

If you know you are looking for the key name of the first element you could simply use jsonPath.getMap("$") of the JSON root, and then get the first key ABC,123 from the Map/Set:

Map<String, String> root = jsonPath.getMap("$");
Set<String> rootKeys = root.keySet();
System.out.println(rootKeys.iterator().next());
wp78de
  • 18,207
  • 7
  • 43
  • 71