You are converting Array of strings into string and adding it to json object

String[] array = {"value1", "value2"};
String arr = Arrays.toString(array)   //converting array into string

Just add the array directly to ObjectNode using putArray

ArrayNode arrayNode = jsonNode.putArray("array");
Arrays.stream(array).forEach(str->arrayNode.add(str));

or you can also use addAll directly by converting array into ArrayNode

jsonNode.putArray("array").addAll(objectMapper.valueToTree(array));
Answer from Ryuzaki L on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-convert-an-arraylist-of-objects-to-a-json-array-in-java
How to Convert an ArrayList of Objects to a JSON Array in Java? - GeeksforGeeks
July 23, 2025 - package org.example; 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) { // Create an ArrayList of objects List<Course> courses = new ArrayList<>(); courses.add( new Course(1, "Java", "25000", "3 Months")); courses.add( new Course(2, "C++", "20000", "3 Months")); // Convert the ArrayList to a JSON array ObjectMapper objectMapper = new ObjectMapper(); try { String jsonArray = objectMapper.writeValueAsString(courses); System.out.println(jsonArray); } catch (JsonProcessingException e) { e.printStackTrace(); } } }
๐ŸŒ
Java67
java67.com โ€บ 2021 โ€บ 12 โ€บ how-to-convert-array-to-json-in-java.html
How to convert array to JSON in Java? Example Tutorial | Java67
You can see by using Gson API and library it's quite easy to convert any type of Java array to JSON messages.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-convert-json-array-object-to-java-object
How to Convert JSON Array Object to Java Object? - GeeksforGeeks
July 23, 2025 - For example, storing a list of items, user profiles, product catalog, etc. JSON arrays allow ordered access to the values using indices like in regular arrays in other languages (0 indexed). ... Iterating and parsing manually. Below are the steps and implementation to convert JSON Array object to Java ...
๐ŸŒ
Kode Java
kodejava.org โ€บ how-do-i-convert-array-into-json
How do I convert array into JSON? - Learn Java by Examples
May 31, 2024 - In the example below you can see how to convert an array into JSON string. We serialize the array to JSON using the Gson.toJson() method. To deserialize a string of JSON into array we use the Gson.fromJson() method. package org.kodejava.gson; ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-convert-Java-Array-Collection-to-JSON-array
How to convert Java Array/Collection to JSON array?
Using this method, you can populate the JSONArray object with the contents of the elements. import org.json.JSONArray; public class ArrayToJson { public static void main(String args[]) { String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"}; JSONArray jsArray = new JSONArray(); for (int i = 0; i < myArray.length; i++) { jsArray.put(myArray[i]); } System.out.println(jsArray); } } ["JavaFX","HBase","JOGL","WebGL"] In the same way you can pass a collection object to the constructor of the JSONArray class.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ convert json object to json array in java
Convert JSON Object to JSON Array in Java | Baeldung
August 24, 2025 - Converting a JSON object to a JSON array is a common requirement in applications that interact with REST APIs, data pipelines, or configuration files. This transformation often becomes necessary when data originally modeled as key-value pairs must be serialized, iterated over, or reformatted for compatibility with front-end frameworks or external systems.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ converting a java list to a json array
Converting a Java List to a Json Array | Baeldung
June 18, 2025 - After adding the required dependency, weโ€™ll use the ObjectMapper class from Jackson to convert the Java List to a JSON array as follows: @Test public void given_JavaList_whenUsingJacksonLibrary_thenOutJsonArray() throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); String jsonArray = objectMapper.writeValueAsString(list); Assert.assertEquals(expectedJsonArray, jsonArray); }
Find elsewhere
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Converting a JSON Object to a JSON Array in Java - Java Code Geeks
August 1, 2025 - Determine whether the result should be an array of objects, an array of values, or a custom structure ยท Be mindful of element ordering, as JSON objects do not guarantee key order ยท You can use the org.json library, which offers straightforward APIs for working with JSON data in Java.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 12188084 โ€บ convert-java-array-object-to-json-using-jackson
Convert Java Array Object to JSON using Jackson - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I need to conver java array object to JSON, i am using jackson for this. I can only think that for every field name i have to iterate from the array which means i have to iterate the same array object for each field.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 42647648 โ€บ how-to-convert-java-object-to-json-object-and-put-in-json-array
How to convert Java object to Json object and put in json array - Stack Overflow
March 7, 2017 - Now i want to send the employee object array kind of thing back to my ajax call. So thought of using JSON. package com;This code is working fine but i am unable to use Employee class and its object.Here i directly used JSON and its working. import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class SelectUsers { public JSONArray selectQueryDemo() throws SQLException { GlobalConnection gc=new GlobalConnection(); Connection conn=gc.getConnection(
๐ŸŒ
Javatpoint
javatpoint.com โ€บ convert-java-object-to-json
Convert Java object to JSON - javatpoint
Convert Java object to JSON with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ converting-json-array-to-a-java-array-or-list
Convert JSON Array to a Java Array or List with Jackson
September 8, 2020 - Created by Guido van Rossum and first released in 1991.'} Language{name='JS', description='JS is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm.'} In this article, we've used Jackson to parse and map the values from a JSON String and file into a Java array and list.
Top answer
1 of 11
714

To convert your object in JSON with Jackson:

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.ObjectWriter; 

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
2 of 11
57

I know this is old (and I am new to java), but I ran into the same problem. And the answers were not as clear to me as a newbie... so I thought I would add what I learned.

I used a third-party library to aid in the endeavor: org.codehaus.jackson All of the downloads for this can be found here.

For base JSON functionality, you need to add the following jars to your project's libraries: jackson-mapper-asl and jackson-core-asl

Choose the version your project needs. (Typically you can go with the latest stable build).

Once they are imported in to your project's libraries, add the following import lines to your code:

 import org.codehaus.jackson.JsonGenerationException;
 import org.codehaus.jackson.map.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;

With the java object defined and assigned values that you wish to convert to JSON and return as part of a RESTful web service

User u = new User();
u.firstName = "Sample";
u.lastName = "User";
u.email = "sampleU@example.com";

ObjectMapper mapper = new ObjectMapper();
    
try {
    // convert user object to json string and return it 
    return mapper.writeValueAsString(u);
}
catch (JsonGenerationException | JsonMappingException  e) {
    // catch various errors
    e.printStackTrace();
}

The result should looks like this: {"firstName":"Sample","lastName":"User","email":"sampleU@example.com"}

๐ŸŒ
Cloudera Community
community.cloudera.com โ€บ t5 โ€บ Support-Questions โ€บ Converting-JSON-to-Java-Object-Array โ€บ td-p โ€บ 153155
Solved: Converting JSON to Java Object Array - Cloudera Community - 153155
February 1, 2017 - Perhaps you should paste your code for suggestion but this website is one I always reference http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ ... Hi @Artem Ervits, yes I checked that too, but I need an Object array which seems to be the only case not covered... ... Error: Can not deserialize instance of java.lang.Object[] out of START_OBJECT token at [Source: {"f1":1,"f2":"abc"}; line: 1, column: 1]