Query a JSONObject in java

I've just unexpectedly found very interesting project: JSON Path

JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document.

With this library you can do what you are requesting even easier, then my previous suggestion:

String hello = JsonPath.read(json, "$.data.data2.value");

System.out.println(hello); //prints hello

Hope this might be helpful either.


While not exactly the same, Jackson has Tree Model representation similar to Gson:

JsonNode root = objectMapper.readTree(jsonInput);
return root.get("data").get("data2").get("value").asText();

so you need to traverse it step by step.

EDIT (August 2015)

There actually is now (since Jackson 2.3) support for JSON Pointer expressions with Jackson. So you could alternatively use:

return root.at("/data/data2/value").asText();