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);
Answer from Jean Logeart on Stack Overflow
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 = "[email protected]";

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":"[email protected]"}

๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ intro to the jackson objectmapper
Intro to the Jackson ObjectMapper | Baeldung
December 9, 2025 - The methods writeValueAsString and writeValueAsBytes of ObjectMapper class generate a JSON from a Java object and return the generated JSON as a string or as a byte array: String carAsString = objectMapper.writeValueAsString(car); Below is a ...
๐ŸŒ
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.
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-json โ€บ jackson-objectmapper.html
Jackson ObjectMapper
The index specifies what element in the array you want to get. It is possible to use the Jackson ObjectMapper to convert a Java object to a JsonNode with the JsonNode being a JSON representation of the Java object converted.
๐ŸŒ
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. If your Java class does not have getters for its fields, ObjectMapper will not be able to access the values, ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ 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(); } } }
Find elsewhere
๐ŸŒ
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 use case involves converting a Java object to JSON. import com.fasterxml.jackson.databind.ObjectMapper; public class ObjectToJsonExample { public static void main(String[] args) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); Employee employee = new Employee(1, "John", "Developer"); // Convert Object to JSON String jsonString = objectMapper.writeValueAsString(employee); System.out.println(jsonString); } }
๐ŸŒ
Squash
squash.io โ€บ how-to-convert-java-objects-to-json-with-jackson
How To Convert Java Objects To JSON With Jackson
Here's an example that demonstrates ... = new Person("John", 30); // Create an instance of ObjectMapper ObjectMapper objectMapper = new ObjectMapper(); // Convert the object to JSON string String jsonString = objectMapper.writeVa...
๐ŸŒ
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 - @Test public void given_JavaList_whenUsingJacksonLibrary_thenOutJsonArray() throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); String jsonArray = objectMapper.writeValueAsString(list); Assert.assertEquals(expectedJsonArray, jsonArray); }
๐ŸŒ
Medium
medium.com โ€บ @ujjawalr โ€บ convert-json-to-object-and-vice-versa-with-jackson-objectmapper-java-be9c47ab5954
Convert JSON To Object And Vice-Versa With Jackson ObjectMapper Java | by Ujjawal Rohra | Medium
August 17, 2024 - This article is a guide to ObjectMapper class from Jackson library and how it is used to convert a json string to java object and a java object to a json string.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ objectmapper
ObjectMapper in Java: Conversion with the Jackson API
March 5, 2024 - In this example, we create an instance of ObjectMapper and use its writeValueAsString method to convert a Java object (myObject) into a JSON string (jsonString).
๐ŸŒ
Folkstalk
folkstalk.com โ€บ home โ€บ 2022 โ€บ september
Objectmapper Convert List Of Objects With Code Examples
September 25, 2022 - We can convert a list to the JSON array using the JSONArray. toJSONString() method and it is a static method of JSONArray, it will convert a list to JSON text and the result is a JSON array.04-Jul-2020 ยท We can easily deserialize JSON Array ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ convert java objects to json with jackson
Convert Java Objects to JSON with Jackson - Mkyong.com
April 24, 2024 - This example uses Jackson to convert a Java object Person to a JSON string. ... package com.mkyong.json.model; public class Person { private String name; private int age; //getters, setters and constructors } ... package com.mkyong.json.jackson.tips; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.mkyong.json.model.Person; public class ConvertObjectToJsonExample { public static void main(String[] args) { Person person = new Person("mkyong", 42); ObjectMapper om = new ObjectMapper(); try { // covert Java object to JSON strings String json = om.writeValueAsString(person); // output: {"name":"mkyong","age":42} System.out.println(json); } catch (JsonProcessingException e) { throw new RuntimeException(e); } } }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-convert-a-json-to-java-object-using-the-jackson-library-in-java
How to convert a JSON to Java Object using the Jackson library in Java?
NOTE: To use the ObjectMapper class, we need to add the Jackson dependency to the pom.xml as shown below - <dependency> <groupId>com.fasterxml.jackson.core </groupId> <artifactId>jackson - databind< /artifactId> <version>2.9.8</version> </dependency> In this program, we create an Employee object and convert it to a formatted JSON string, then deserialize it. import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.databind.ObjectMapper; public class JSONToJavaObjectTest { public static void main(String args[]) throws IOException { Employee emp
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ convert java object to json
Convert Java object to JSON - Tabnine
July 25, 2024 - Use the JACKSON API: ObjectMapper mapper class: call writeValueAsString(ObjToConvert) method by passing the object we want to convert into JSON or Use GSON API: class Gson: call toJson(ObjToConvert) method by passing the object we want to convert ...
๐ŸŒ
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 the last two lines, you can see we create a new ObjectMapper instance, and use it to serialize (writeValueAsString) the Map value to a JSON string, which is outputted to the console.
๐ŸŒ
Medium
medium.com โ€บ @salvipriya97 โ€บ how-to-convert-java-pojo-to-json-using-jackson-2ef9b69c4f08
How to convert Java POJO to JSON using Jackson | by Priya Salvi | Medium
April 28, 2024 - We use the writeValueAsString() method of the ObjectMapper instance to serialize the Person object to a JSON string.