Jackson – Convert JSON Array of Objects to List

Hello everyone, Here we will show how to convert JSON array String to List

The Jackson library is composed of three components: Jackson Databind, Core, and Annotation. Jackson Databind has internal dependencies on Jackson Core and Annotation. Therefore, adding Jackson Databind to your Maven POM dependency /Gradle list will include the other dependencies as well.

Maven (pom.xml) 

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>

Gradle (build.gradle) 

implementation group: 'com.fasterxml.jackson.core',
name: 'jackson-core', version: '2.13.2'


JSON Array of Objects

[
{
"name": "knowledgefactory1",
"salaray": 1000
},
{
"name": "knowledgefactory2",
"salaray": 1200
}
]



Create an object to map the above JSON fields.

package com.knf;

public class Employee {

String name;
Double salaray;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getSalaray() {
return salaray;
}

public void setSalaray(Double salaray) {
this.salaray = salaray;
}

@Override
public String toString() {
return "Employee [name=" + name + ",
salaray=" + salaray + "]";
}
}



Convert the JSON array string to a List

package com.knf;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class JacksonArrayExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String json =
"[{\"name\":\"knowledgefactory1\", \"salaray\":1000},
{\"name\":\"knowledgefactory2\", \"salaray\":1200}]";
try {
// 1. convert JSON array to Array objects
Employee[] pp1 = mapper.
readValue(json, Employee[].class);
System.out.println("JSON array to Array objects...");
for (Employee person : pp1) {
System.out.println(person);
}
// 2. convert JSON array to List of objects
List<Employee> ppl2 = Arrays.asList(mapper.readValue
(json, Employee[].class));
System.out.println("\nJSON array to List of objects");
ppl2.stream().forEach(x -> System.out.println(x));
// 3. alternative
List<Employee> pp3 = mapper.readValue(json,
new TypeReference<List<Employee>>() {});
System.out.println("\nAlternative...");
pp3.stream().forEach(x -> System.out.println(x));

} catch (IOException e) {
e.printStackTrace();
}
}
}



Output:

JSON array to Array objects...
Employee [name=knowledgefactory1, salaray=1000.0]
Employee [name=knowledgefactory2, salaray=1200.0]
JSON array to List of objects
Employee [name=knowledgefactory1, salaray=1000.0]
Employee [name=knowledgefactory2, salaray=1200.0]
Alternative...
Employee [name=knowledgefactory1, salaray=1000.0]
Employee [name=knowledgefactory2, salaray=1200.0]


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete