Convert JSON Array String to List and List to JSON Array String with Jackson

In this section, we will show you how to convert JSON Array String to List and List to JSON Array String with Jackson.

Note


Jackson is a very popular and efficient java based library to serialize or map java objects to JSON and vice versa.

Note


JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging.

JSON is built on two structures: 

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. 
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

1. Download Jackson

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

1. Convert JSON Array String to List

User.java

public class User {

private String name;
private String email;

public String getName() {
return name;
}

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

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}


Main.java

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 Main {

public static void main(String[] args) {

ObjectMapper mapper = new ObjectMapper();
String json = "[{\"name\":\"sibin\", " +
"\"email\":\"sibin@gmail.in\"}," +
" {\"name\":\"tom\", " +
"\"email\":\"tom@gmail.in\"}]";

try {

// 1. convert JSON array to Array objects
User[] user1 = mapper.readValue(json, User[].class);

System.out.println("JSON array to Array objects...");
for (User user : user1) {
System.out.println(user);
}

// 2. convert JSON array to List of objects
List<User> user2 = Arrays.asList(mapper.
readValue(json, User[].class));

System.out.println("\nJSON array to List of objects");
user2.stream().forEach(o -> System.out.println(o));

// 3. alternative
List<User> user3 = mapper.readValue(json,
new TypeReference<List<User>>() {});

System.out.println("\nAlternative...");
user3.stream().forEach(o -> System.out.println(o));

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

}
}

Console Output:
JSON array to Array objects...
User{name='sibin', email='sibin@gmail.in'}
User{name='tom', email='tom@gmail.in'}

JSON array to List of objects
User{name='sibin', email='sibin@gmail.in'}
User{name='tom', email='tom@gmail.in'}

Alternative...
User{name='sibin', email='sibin@gmail.in'}
User{name='tom', email='tom@gmail.in'}

2. List to JSON Array String

Main.java

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {

ObjectMapper mapper = new ObjectMapper();

User user1=new User();
user1.setEmail("sibin@gmail.in");
user1.setName("sibin");

User user2=new User();
user2.setEmail("tom@gmail.in");
user2.setName("tom");

List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);

try {

// convert List to JSON string
String json1 = mapper.writeValueAsString(users);

System.out.println(json1); // compact-print

// convert List to JSON string - pretty print
String json2 = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(users);

System.out.println(json2);

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


Console Output:
[{"name":"sibin","email":"sibin@gmail.in"},{"name":"tom","email":"tom@gmail.in"}]
[ {
  "name" : "sibin",
  "email" : "sibin@gmail.in"
}, {
  "name" : "tom",
  "email" : "tom@gmail.in"
} ]

Comments

Popular posts from this blog

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

ReactJS - Bootstrap - Buttons

Spring Core | BeanFactoryPostProcessor | Example

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

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

Top 5 Java ORM tools - 2024

Java - DES Encryption and Decryption example