Parsing Nested JSON in java without know structure of JSON

213 Views Asked by At

I have a use case where i get a random jsonstring and variable name. I need to verify if that particular variable is present in that json, and if present fetch its value. For example, let us the json is as follows

{
   "a" : {
        "b":1,
        "c":2
   }
}

Along with above jsonString, say i get an input "a.b" . Now I need to return 1. Is there any library to achieve this in java directly?

1

There are 1 best solutions below

0
Kraylog On BEST ANSWER

JsonPath is a library that provides the functionality you're after.

You will have to do some conversion between your input and the library's input.

As per your example, if your input is "a.b":

String convertedInput = ".." + input
JsonPath.read(json, convertedInput)