Jackson JsonNode to typed Collection

We Are Going To Discuss About Jackson JsonNode to typed Collection. So lets Start this Java Article.

Jackson JsonNode to typed Collection

  1. Jackson JsonNode to typed Collection

    Acquire an ObjectReader with ObjectMapper#readerFor(TypeReference) using a TypeReference describing the typed collection you want. Then use ObjectReader#readValue(JsonNode) to parse the JsonNode (presumably an ArrayNode).

  2. Jackson JsonNode to typed Collection

    Acquire an ObjectReader with ObjectMapper#readerFor(TypeReference) using a TypeReference describing the typed collection you want. Then use ObjectReader#readValue(JsonNode) to parse the JsonNode (presumably an ArrayNode).

Solution 1

Acquire an ObjectReader with ObjectMapper#readerFor(TypeReference) using a TypeReference describing the typed collection you want. Then use ObjectReader#readValue(JsonNode) to parse the JsonNode (presumably an ArrayNode).

For example, to get a List<String> out of a JSON array containing only JSON strings

ObjectMapper mapper = new ObjectMapper();
// example JsonNode
JsonNode arrayNode = mapper.createArrayNode().add("one").add("two");
// acquire reader for the right type
ObjectReader reader = mapper.readerFor(new TypeReference<List<String>>() {
});
// use it
List<String> list = reader.readValue(arrayNode);
Advertisements

Original Author Sotirios Delimanolis Of This Content

Solution 2

If an Iterator is more useful…

…you can also use the elements() method of ArrayNode. Example see below.

sample.json

{
    "first": [
        "Some string ...",
        "Some string ..."
    ],
    "second": [
        "Some string ..."
    ]
}

So, the List<String> is inside one of the JsonNodes.

Java

When you convert that inner node to an ArrayNode you can use the elements() method, which returns an Iterator of JsonNodes.

File file = new File("src/test/resources/sample.json");
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(file);
ArrayNode arrayNode = (ArrayNode) jsonNode.get("first");
Iterator<JsonNode> itr = arrayNode.elements();
// and to get the string from one of the elements, use for example...
itr.next().asText();

New to Jackson Object Mapper?

I like this tutorial:
https://www.baeldung.com/jackson-object-mapper-tutorial

Update:

You can also use .iterator() method of ArrayNode. It is the same:

Same as calling
.elements(); implemented so that convenience “for-each” loop can be
used for looping over elements of JSON Array constructs.

from the javadocs of com.fasterxml.jackson.core:jackson-databind:2.11.0

Advertisements

Original Author Mue Of This Content

Solution 3

The ObjectMapper.convertValue() function is convenient and type-aware. It can perform a wide array of conversions between tree nodes and Java types/collections, and vice-versa.

An example of how you might use it:

List<String> list = new ArrayList<>();
list.add("one");
list.add("two");

Map<String,List<String>> hashMap = new HashMap<>();
hashMap.put("myList", list);

ObjectMapper mapper = new ObjectMapper();
ObjectNode objectNode = mapper.convertValue(hashMap, ObjectNode.class);
Map<String,List<String>> hashMap2 = mapper.convertValue(objectNode, new TypeReference<Map<String, List<String>>>() {});
Advertisements

Original Author DanJ Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment