Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve strings from JSON array using Jackson

Tags:

java

json

jackson

I've found several answers that are close to what I'm trying to do, but not enough that I've been able to get it to work. I have a bunch of JSON that looks like this example (only actually several levels deeper and with hundreds of items at the level I want to access): {"query":{"pages":{"links":[{"word":"bobsyeruncle","code":4},{"word":"easyaspie","code":3}]}}}. I can't change the format; it's someone else's API. There's quite a lot of this that I don't need; in fact I only want something like an array of ["bobsyeruncle","easyaspie"]. (Or a List or whatever.)

I experimented with a simpler version of the JSON that didn't have an array, and was able to access a single string easily using the rootNode.get("query").get("pages")... way described in https://stackoverflow.com/questions/338586/a-better-java-json-library/338608#338608. But I haven't been able to get at the array that way. Most of the answers I've found here assume that I want to create a POJO like "Links" that includes both "word" and "code," which I don't. In order to access the strings I want, is it necessary to create something like a list of "Links" that include both "word" and "code" and then ignore "code"? That doesn't seem right.

(Also, if anyone could point me to documentation/tutorials somewhere in between the JacksonInFiveMinutes tutorial and the whole javadoc, I'm sure that would help too.)

ETA this worked, I think!:

            String theJsonString = "{\"query\":{\"pages\":{\"links\":"
                + "[{\"word\":\"bobsyeruncle\"},{\"word\":\"easyaspie\"}]}}}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(theJsonString);
        JsonNode interestingObjectNode = rootNode.path("query").path("pages").path("links");
        for (int i = 0; i < interestingObjectNode.size(); i ++) {
            System.out.println(interestingObjectNode.get(i).path("word").asText());
        }
like image 525
umbraphile Avatar asked Nov 20 '11 17:11

umbraphile


People also ask

How does Jackson read JSON array?

Reading JSON from a File Thankfully, Jackson makes this task as easy as the last one, we just provide the File to the readValue() method: final ObjectMapper objectMapper = new ObjectMapper(); List<Language> langList = objectMapper. readValue( new File("langs. json"), new TypeReference<List<Language>>(){}); langList.

How do I read a specific JsonNode in Jackson API?

Jackson JSON - Edit JSON DocumentreadAllBytes(Paths. get("employee. txt")); ObjectMapper objectMapper = new ObjectMapper(); //create JsonNode JsonNode rootNode = objectMapper. readTree(jsonData); //update JSON data ((ObjectNode) rootNode).

Can we convert JSON array to string?

Stringify a JavaScript ArrayUse the JavaScript function JSON.stringify() to convert it into a string.


1 Answers

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!

like image 95
StaxMan Avatar answered Sep 28 '22 08:09

StaxMan