The correct json path expression would be "/aaa/0/value1"
Use:
jsonNode.at("/aaa/0/value1")
Answer from S.K. on Stack OverflowThe correct json path expression would be "/aaa/0/value1"
Use:
jsonNode.at("/aaa/0/value1")
use this below code :
JsonNode node = mapper.readTree(json);
System.out.println(node.path("aaa").get(0)); // {"value1":"123"}
- use jackson-databind.
- use this
node.path("aaa").get(0).get("value1") // 123.
Perhaps this blog entry might help: Traversing JSON trees with Jackson?
I am not sure which exact problem you have, but one thing to note is that JSON Arrays are traversed by passing index of entry, NOT the name. So whereas you would use objectNode.get("key"), you use arrayNode.get(0) instead.
Or, if you want to play safe and allow "missing" entries, use arrayNode.path(0) (and ditto for JSON Objects).
Also remember that you can go back between JSON Trees (JsonNode) and POJOs; ObjectMapper has multiple methods for converting between representations (convertValue(), readAsTree(), treeToValue(), valueToTree()). So it is possible to use data-binding for some parts, tree model for others; sometimes binding sub-trees as POJOs, other times just data-binding high-level and accessing sub-trees using tree model. This is a very powerful way to do things, but takes a while getting used to.
Hope this helps!
In Google's GSON if you create a POJO that lacks some properties, then the coresponding JSON is ignored. It populates only those properties that have matching names. Why not create a class like this :
Query{
Pages{
Word[] Links;
}
}
Word{
String word;
String code;
}
and then use LambdaJ to avoid writing all the loops to get the words?
If that is not attractive look here and try JSONPath
Lots of document databases out there like MongoDB and RavenDB etc use JSON as their format for storage. Querying complex JSON is built into them, use the same libraries that they are using.
What you are trying to do is also called as Xpath in technical terms. It is used for html, xml based languages as well as now available in json
You can try jsonpath for this case:
https://www.baeldung.com/guide-to-jayway-jsonpath https://github.com/json-path/JsonPath
Google's popular GSON library has a method, namely getPath, maybe useful for your purpose:
String json = "...";
JsonReader reader = new JsonReader(new StringReader(json));
System.out.println(reader.getPath());