
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert List to JSON Array in Java
Converting a list to a JSON array is one of the common tasks in Java. Let's see how to convert a list to a JSON array in Java. If you do not know what JSON is, then you can read the JSON tutorial. The following are various ways to convert a list to a JSON array in Java:
- Manual construction of JSON array
- Using Jackson library
- Using Gson library
- Using org.json library
Let's learn the process of each of them one by one.
Manual construction of JSON array
Let's see how to convert a list to a JSON array without any dependency. Steps to convert a list to a JSON array:
- First, we will create a list of strings.
- Then we will create a JSON array using the StringBuilder class.
- Next, we will iterate through the list and append each element to the JSON array.
- Finally, we will print the JSON array.
Example
Here is the code to convert a list to a JSON array manually:
import java.util.Arrays; import java.util.List; public class ManualJsonArray { public static void main(String[] args) { List<String> list = Arrays.asList("apple", "banana", "cherry"); StringBuilder jsonArray = new StringBuilder("["); for (int i = 0; i < list.size(); i++) { jsonArray.append(""").append(list.get(i)).append("""); if (i < list.size() - 1) jsonArray.append(", "); } jsonArray.append("]"); System.out.println("JSON Array: " + jsonArray.toString()); } }
Following is the output of the above code:
JSON Array: ["apple", "banana", "cherry"]
Using the Jackson library
Jackson is a library that provides a way to convert Java objects to JSON and vice versa.
It is a third-party library, so we will need to add this library to our project manually by downloading it from the official website. If you are using Maven, add this to your pom.xml file:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency>
Steps to convert a list to a JSON array using the Jackson library:
- Import the com.fasterxml.jackson.databind.ObjectMapper and com.fasterxml.jackson.databind.node.ArrayNode classes.
- First, we will create a list of strings.
- Then we will create an instance of the ObjectMapper class.
- Next, we will convert the list to a JSON array using the valueToTree() method of ObjectMapper.
- We will use the ArrayNode class to represent the JSON array.
- We will use the toPrettyString() method to convert the JSON array to a string.
- Finally, we will print the JSON array.
Example
Following is the code to convert a list to a JSON array using the Jackson library:
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import java.util.Arrays; import java.util.List; public class ListToJsonArrayJackson { public static void main(String[] args) throws Exception { List<String> list = Arrays.asList("apple", "banana", "cherry"); ObjectMapper mapper = new ObjectMapper(); ArrayNode arrayNode = mapper.valueToTree(list); System.out.println("JSON Array: " + arrayNode.toPrettyString()); } }
Following is the output of the above code:
JSON Array: [ "apple", "banana", "cherry" ]
Using the Gson library
Gson is a library that provides a way to convert Java objects to JSON and vice versa. To use this library, you will need to add it to your project. You can simply download it from the official website, or if you are using Maven, add this to your pom.xml file:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
Steps to convert a list to a JSON array using the Gson library:
- First, import the Gson and its JsonArray class.
- Then, create a list of strings.
- Then we will create an instance of the Gson class.
- Next, we will convert the list to a JSON array using the toJsonTree() method of Gson.
- Finally, we will print the JSON array.
Example
Following is the code to convert a list to a JSON array using the Gson library:
import com.google.gson.Gson; import com.google.gson.JsonArray; import java.util.List; import java.util.Arrays; public class ListToJsonArrayGson { public static void main(String[] args) { Listlist = Arrays.asList("apple", "banana", "cherry"); Gson gson = new Gson(); JsonArray jsonArray = gson.toJsonTree(list).getAsJsonArray(); System.out.println("JSON Array: " + jsonArray); } }
Following is the output of the above code:
JSON Array: ["apple","banana","cherry"]
Using the org.json library
This is a commonly used library for JSON manipulation in Java. It is a third-party library, so we will need to add this library to our project manually by downloading it from the official website. If you are using Maven, add this to your pom.xml file:
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20210307</version> </dependency>
Steps to convert a list to a JSON array using the org.json library:
- First, import the org.json.JSONArray class.
- Then, create a list of strings.
- Next, create a JSONArray object and pass the list to its constructor.
- Finally, print the JSON array using the toString() method.
Example
The following is the code to convert a list to a JSON array using the org.json library:
import org.json.JSONArray; import java.util.Arrays; import java.util.List; public class ListToJsonArrayOrg { public static void main(String[] args) { List<String> list = Arrays.asList("apple", "banana", "cherry"); JSONArray jsonArray = new JSONArray(list); System.out.println("JSON Array: " + jsonArray.toString(2)); } }
Following is the output of the above program -
JSON Array: [
"apple",
"banana",
"cherry"
]