This is overly complicated, Jackson handles lists via its writer methods just as well as it handles regular objects. This should work just fine for you, assuming I have not misunderstood your question:

public void writeListToJsonArray() throws IOException {  
    final List<Event> list = new ArrayList<Event>(2);
    list.add(new Event("a1","a2"));
    list.add(new Event("b1","b2"));

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ObjectMapper mapper = new ObjectMapper();

    mapper.writeValue(out, list);

    final byte[] data = out.toByteArray();
    System.out.println(new String(data));
}
Answer from Perception on Stack Overflow
๐ŸŒ
Makeinjava
makeinjava.com โ€บ home โ€บ convert list of objects to/from json in java (jackson objectmapper/ example)
Convert list of objects to/from JSON in java (jackson objectmapper/example)
January 1, 2024 - Convert List of Person objects to JSON String arrayToJson = objectMapper.writeValueAsString(personList); System.out.println("1. Convert List of person objects to JSON :"); System.out.println(arrayToJson); //2.
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ convert-list-to-json-java-objectmapper
Converting a List to JSON in Java using ObjectMapper โ€” javathinking.com
We instantiate an ObjectMapper object. Finally, we use the writeValueAsString() method to convert the list to a JSON string and print it.
Discussions

What is the best way to write a Java list to a JSON array - Stack Overflow
I want to use Jackson to convert an ArrayList to a JsonArray. Event is the Java bean class with two fields "field1", "field2" mapped as JsonProperty. My goal is: Convert ArrayLi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to use Jackson to deserialise an array of objects - Stack Overflow
The Jackson data binding documentation indicates that Jackson supports deserialising "Arrays of all supported types" but I can't figure out the exact syntax for this. For a single object I would do More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Convert List<Object[]> to JSON - Stack Overflow
Hi guys! Can someone helps me, how to convert my HQL query results to JSON with list of objects and get it with rest service. Here is my service method which returns qyery result list: @Over... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How can I convert JSON file into List of objects? - Stack Overflow
I'm trying to do a loader for my program and the saver into json works, but the reader doesn't. I'm basically converting my list of objects, let's say ArrayList ProjectArr into json which is working, now I'm gonna close terminal and load the info into my initially blank ProjectArr list so I can add the saved data loading feature. here is my reader : public static ArrayList MapReaderforProject() { ObjectMapper ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-convert-a-list-to-json-array-using-the-jackson-library-in-java
How to convert a List to JSON array using the Jackson library in Java?
April 29, 2025 - It allows you to build a JSON array node-by-node using the ArrayNode class. import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.node.ArrayNode; public class ListToJSONArrayUsingArrayNode { public static void main(String[] args) { List < String > list = Arrays.asList("JAVA", "PYTHON", "SCALA", ".NET", "TESTING"); ObjectMapper objectMapper = new ObjectMapper(); ArrayNode arrayNode = objectMapper.createArrayNode(); for (String item: list) { arrayNode.add(item); // adding each item manually } try { String jsonArray = objectMapper.writeValueAsString(arrayNode); System.out.println(jsonArray); } catch (Exception e) { e.printStackTrace(); } } }
๐ŸŒ
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 - If we are seeking to use a powerful and flexible API, Jackson simplifies the task of converting Java objects to JSON and vice versa. To use Jackson, letโ€™s first add the following dependency to the pom.xml file: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.15.2</version> </dependency> 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); } In the provided code snippet, we instantiate the ObjectMapper class and utilize its writeValueAsString() method.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2019 โ€บ 07 โ€บ convert-list-to-json-array-using-jackson.html
Convert List to JSON Array Using Jackson
July 21, 2019 - The following example shows how to convert List object to JSON array using the ObjectMapper.writeValueAsString() method.
๐ŸŒ
Spring Framework Guru
springframework.guru โ€บ home โ€บ how to the jackson object mapper with json
How to The Jackson Object Mapper with JSON - Spring Framework Guru
October 22, 2024 - In this blog post, Iโ€™ll walk you through how to get started with Jacksonโ€™s ObjectMapper, its key features, and best practices for its use in Java projects. Jackson is a high-performance, flexible JSON processing library for Java. It known for its ease of use and integration with popular frameworks like Spring Boot. Jackson supports converting Java objects to JSON (serialization), and JSON to Java objects (deserialization).
Find elsewhere
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-json โ€บ jackson-objectmapper.html
Jackson ObjectMapper
You convert a Java object to a JsonNode via the Jackson ObjectMapper valueToTree() method.
๐ŸŒ
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 ... "20000", "3 Months")); // Convert the ArrayList to a JSON array ObjectMapper objectMapper = new ObjectMapper(); try { String jsonArray = objectMapper.writeValueAsString(courses); System.out...
๐ŸŒ
Medium
medium.com โ€บ @vino7tech โ€บ handling-json-with-objectmapper-in-spring-boot-6fc7ff39088b
Handling JSON with ObjectMapper in Spring Boot | by Vinotech | Medium
October 23, 2024 - This guide explores how to use ObjectMapper in a Spring Boot application for converting Java objects to JSON and vice versa. It covers key use cases like customizing JSON field names, handling unknown properties, working with lists, and configuring ObjectMapper for special scenarios like date ...
Top answer
1 of 2
4

Just return the list from the controller method

@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public List<Object[]> getAllDeps() {
    List<Object[]> list  = departmentService.getAllDepartments();
    return list;
}

The @ResponseBody annotation does the transformation for you.

2 of 2
0
public JSONObject findOccurrByFileUUID(UUID filePefin) throws JSONException {

    StringBuilder strQuery = new StringBuilder();
    strQuery.append("SELECT c.\"name\" AS creditorname, d.\"name\" AS debtorname, concat(b2.\"type\",'-',b2.\"number\",'-',b2.parcel) AS docto, b2.uniquenumber, to_char(b.dateoccurr,'DD/MM/YYYY HH24:MI:SS') AS dateoccur, to_char(b.createddate, 'DD/MM/YYYY HH24:MI:SS') AS createddate, b.statusnegatived, l.username FROM billnegativedoccurr b \n" +
                    "LEFT JOIN billnegatived b2 ON b2.uuid = b.billnegativeduuid \n" +
                    "LEFT JOIN creditor c ON c.tenantowner_uuid = b2.tenant_uuid AND c.uuid = b2.creditor_uuid \n" +
                    "LEFT JOIN debtor d ON d.tenantowner_uuid = b2.tenant_uuid AND d.uuid = b2.debtor_uuid \n" +
                    "INNER JOIN login l ON l.uuid = b.useroccurr \n" +
                    "WHERE b.filepefinuuid = :filePefin");

    Query query = eM.createNativeQuery(strQuery.toString()); //no entity mapping
    query.setParameter("filePefin", filePefin);
    List<Object[]> queryList = query.getResultList();
    JSONArray array = new JSONArray();
    JSONObject obj = new JSONObject();

    for (Object[] result : queryList) {

        JSONObject object = new JSONObject();
        object.put("creditorName", result[0]);
        object.put("debtorName", result[1]);
        object.put("docto", result[2]);
        object.put("uniquenumber", result[3]);
        object.put("dateoccur", result[4]);
        object.put("createddate", result[5]);
        object.put("statusnegatived", result[6]);
        object.put("username", result[7]);

        array.put(object);
    }

    return obj.put("Values", array);
}
๐ŸŒ
Squash
squash.io โ€บ how-to-convert-java-objects-to-json-with-jackson
How To Convert Java Objects To JSON With Jackson
November 8, 2023 - Here are two common techniques: ... JSON and vice versa. To convert a Java object to JSON, you can use the writeValueAsString() method of the ObjectMapper class....
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Convert Java List Object Into JSON String using Jackson API | Java Inspires - YouTube
In this video, we will see how to convert(serialize) Java List Object into JSON String and vice versa means how to convert(deserialize) JSON String to Java ...
Published: June 7, 2021
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to parse json array with jackson
How to parse JSON Array with Jackson | mkyong.com
April 23, 2024 - In Jackson, we can convert the JSON array to an Array or List. ... package com.mkyong.json.jackson; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.mkyong.json.model.Person; import java.util.List; public class JsonArrayToObjectExample { public static void main(String[] args) throws JsonProcessingException { String jsonArray = "[{\"name\":\"mkyong\", \"age\":42}, {\"name\":\"ah pig\", \"age\":20}]"; ObjectMapper mapper = new ObjectMapper(); // 1.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Intro to JSON and Jackson's ObjectMapper | Parse JSON in Java | Convert Object to JSON | Geekific - YouTube
Discord Community: https://discord.gg/dK6cB24ATpGitHub Repository: https://github.com/geekific-official/ If youโ€™re into coding, and no matter what language y...
Published: October 15, 2022
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 67858246 โ€บ how-can-i-convert-json-file-into-list-of-objects
java - How can I convert JSON file into List of objects? - Stack Overflow
I'm trying to do a loader for my program and the saver into json works, but the reader doesn't. I'm basically converting my list of objects, let's say ArrayList ProjectArr into json which is working, now I'm gonna close terminal and load the info into my initially blank ProjectArr list so I can add the saved data loading feature. here is my reader : public static ArrayList<Project> MapReaderforProject() { ObjectMapper objectMapperforProject = new ObjectMapper(); String jsonfile = "C:/Users/elmm/Desktop/DS Project/DsProject/target/test1.json"; File file = new File("C:/Users/elmm/Desktop/DS Project/DsProject/target/test1.json"); try { ProjectArr = objectMapperforProject.readValue(jsonfile,new TypeReference<ArrayList<Project>>(){}); } catch (JsonMappingException e) { } catch (JsonProcessingException e) { } System.out.println(ProjectArr.toString()); return ProjectArr; }
Top answer
1 of 4
3

Why are you putting your list into Map? Code looks weird. If you want to return a list, just do it:

@RequestMapping("/getGodowns")
public @ResponseBody List<CscGodownBean> getGodownsBasedOnDistrict(@RequestParam(value="district_code") String dist_code) {
    List<CscGodownBean> godown_list = null;
    String exception = null;
    try {
        //getting name and codes here
        godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
    } catch (Exception ex) {
        ex.printStackTrace();
        exception = ex.getMessage();
    }
    return godown_list;
}
2 of 4
2

Change the return result from Map to List<CscGodownBean> and put : retrun godown_list So;

@RequestMapping("/getGodowns")
public @ResponseBody List<CscGodownBean>
getGodownsBasedOnDistrict(@RequestParam(value="district_code") String 
dist_code) {

List<CscGodownBean> godown_list = new ArrayList<CscGodownBean>();
String exception = null;
try
{
    //getting name and codes here
    godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
   ex.printStackTrace();
   exception = ex.getMessage();
}

return godown_list ;
}

UPDATE

And you can return result as string and you will get what you need :

@RequestMapping("/getGodowns")
public @ResponseBody String
getGodownsBasedOnDistrict(@RequestParam(value="district_code") String 
dist_code) {

List<CscGodownBean> godown_list = new ArrayList<CscGodownBean>();
String exception = null;
try
{
    //getting name and codes here
    godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
   ex.printStackTrace();
   exception = ex.getMessage();
}
ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    String arrayToJson = objectMapper.writeValueAsString(godown_list);
    System.out.println("Convert List to JSON :");
    System.out.println(arrayToJson);

return arrayToJson ;
}

The returned String is json format.

๐ŸŒ
Medium
samedesilva.medium.com โ€บ jackson-objectmapper-in-rest-assured-8511dd62c608
Jackson ObjectMapper in Rest-Assured | by Sameera De Silva | Medium
March 24, 2025 - Perfect for handling JSON arrays when you donโ€™t want to create a POJO. Works well when data structure varies dynamically. ... import com.fasterxml.jackson.databind.ObjectMapper; public class ArrayToJsonExample { public static void main(String[] args) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); // Java array String[] names = {"morpheus", "neo", "trinity"}; // ๐Ÿ”น Serialization: Convert Java array to JSON string String jsonArray = objectMapper.writeValueAsString(names); System.out.println("Serialized JSON from Array: " + jsonArray); } }