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
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ intro to the jackson objectmapper
Intro to the Jackson ObjectMapper - JSON
December 9, 2025 - This tutorial focuses on understanding the Jackson ObjectMapper class and how to serialize Java objects into JSON and deserialize JSON string into Java objects.
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"}

Discussions

JSON string to Java object with Jackson - Stack Overflow
This is probably one of those questions where the title says it all. I am quite fascinated by the ObjectMapper's readValue(file, class) method, found within the Jackson library which reads a JSON ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java objects to JSON
Depends, some network libraries will probably handle the serialisation in the background but you can also use things like GSON and Jackson to do the serialisation yourself More on reddit.com
๐ŸŒ r/javahelp
11
9
May 22, 2021
Convert Java object to JSON without external library
Converting arbitrary objects to JSON isn't particularly easy, especially when there are full solutions already available. Personally, I use Gson, which is pretty easy to use. If the no external libraries is a HARD requirement, then your only choice would be to use reflection to get the names and values of the fields of the object to build the JSON string. You might have recursion problems if there are nested objects that create a circular loop, which complicates things. If you only have to serialize specific classes, then it would probably be easier to essentially hardcode the JSON string generation, and just call the appropriately getters to build the string. Basically, if it's only a few specific classes then you can write your own, if it's any arbitrary object then I highly recommend using a library, but if no libraries then I guess you could try homerolling your own JSON serializer with reflection. More on reddit.com
๐ŸŒ r/javahelp
12
1
May 1, 2020
How to convert object to json in controller?
What if Jackson doesnโ€™t recognize my Revision object and canโ€™t serialize it? More on reddit.com
๐ŸŒ r/SpringBoot
6
6
September 2, 2022
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ convert java objects to json with jackson
Convert Java Objects to JSON with Jackson | mkyong.com
April 24, 2024 - In quick, we use Jacksonโ€™s ObjectMapper object to convert Java object to / from JSON.
๐ŸŒ
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 - Adding the jackson-databind dependency to your project adds two transitive dependencies: ... Serialization is the process of converting a Java object into JSON. Remember, in Java everything is an object.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-java-object-to-json-string-using-jackson-api
Convert Java Object to Json String using Jackson API - GeeksforGeeks
March 14, 2026 - package com.Geeks; import com.fasterxml.jackson.databind.ObjectMapper; public class ObjectToJson { public static void main(String[] args) { // Creating Organisation object Organisation org = getObjectData(); // Creating ObjectMapper instance ObjectMapper mapper = new ObjectMapper(); try { // Converting Java object to JSON string String jsonString = mapper.writeValueAsString(org); System.out.println(jsonString); } catch (Exception e) { e.printStackTrace(); } } // Method to populate object data public static Organisation getObjectData() { Organisation org = new Organisation(); org.setOrganisation_name("GeeksforGeeks"); org.setDescription("A Computer Science portal for Geeks"); org.setEmployees(2000); return org; } }
๐ŸŒ
Oracle
blogs.oracle.com โ€บ javamagazine โ€บ java-json-serialization-jackson
Looking for a fast, efficient way to serialize and share Java objects? Try Jackson.
Jackson can be used to automatically serialize this class to JSON so that it can, for example, be sent over the network to another service that may or may not be implemented in Java and that can receive JSON-formatted data.
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-json โ€บ jackson-objectmapper.html
Jackson ObjectMapper
Generating JSON from Java objects is also referred to as to serialize Java objects into JSON. The Jackson Object mapper can parse JSON into objects of classes developed by you, or into objects of the built-in JSON tree model explained later in this tutorial.
Find elsewhere
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ jackson-convert-java-object-to-from-json
Convert Java Object to JSON using Jackson
October 14, 2022 - Jackson provides writeValue() and readValue() methods to convert Java Objects to and from JSON.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ jackson โ€บ jackson โ€“ marshal and unmarshal java objects to json
Jackson - Marshal and Unmarshal Java Objects to JSON
August 20, 2022 - After importing Jackson, we can start using ObjectMapper class for marshalling and unmarshalling the JSON. ObjectMapper mapper = new ObjectMapper(); //Java Object -> JSON String json = mapper.writeValueAsString(pojoInstance); //JSON -> Java ...
๐ŸŒ
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 - Create an instance of the ObjectMapper class from Jackson. Use the writeValueAsString() method of the ObjectMapper instance to serialize the Java object to JSON format.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ jackson-json-java-parser-api-example-tutorial
Jackson JSON Java: Parser API Examples & Tutorial | DigitalOcean
Parse JSON in Java using Jackson library. Complete tutorial with examples for serialization, deserialization, annotations, and advanced JSON processing.
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ convert java object to json
Convert Java object to JSON - Tabnine
July 25, 2024 - We can use the ObjectMapper class provided by the Jackson API for our conversion: writeValueAsString() is used to convert java obj to JSON
๐ŸŒ
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.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ jackson-convert-java-object-to-json-node
Convert Java Object to JsonNode using Jackson
October 14, 2022 - To convert the above User class instance to a JsonNode object, you can use the valueToTree() method from ObjectMapper, as shown below:
๐ŸŒ
Reflectoring
reflectoring.io โ€บ jackson
All You Need To Know About JSON Parsing With Jackson
July 14, 2022 - Full Data Binding which Converts JSON to and from any Java class. ObjectMapper is the most commonly used part of the Jackson library as itโ€™s the easiest way to convert between POJOs and JSON.
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ convert_java_object_to_jsonnode_in_jackson_duplicate
Convert Java Object to JsonNode in Jackson
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
DEV Community
dev.to โ€บ sadiul_hakim โ€บ jackson-tutorial-comprehensive-guide-with-examples-2gdj
Jackson Tutorial: Comprehensive Guide with Examples - DEV Community
September 18, 2025 - import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; ObjectMapper mapper = new ObjectMapper(); // Create an empty object node ObjectNode personNode = mapper.createObjectNode(); // Add properties to the object personNode.put("name", "John"); personNode.put("age", 30); personNode.put("isStudent", false); // Create an array node ArrayNode hobbiesNode = mapper.createArrayNode(); hobbiesNode.add("Reading"); hobbiesNode.add("Swimming"); hobbiesNo
๐ŸŒ
DevGenius
blog.devgenius.io โ€บ converting-java-objects-to-json-with-jackson-a-simple-guide-df8263e10ea9
Converting Java Objects to JSON with Jackson โ€” A Simple Guide | by M Business Solutions | Dev Genius
December 4, 2024 - Turning Java objects into JSON is a common task, and Jackson makes it easy! Jackson is like a magician for JSON, helping you convert your Java objects to JSON format in just a few lines.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ mapping a dynamic json object with jackson
Mapping a Dynamic JSON Object with Jackson | Baeldung
January 8, 2024 - In this quick tutorial, weโ€™ll learn multiple ways of mapping dynamic JSON objects into Java classes. Note that in all of the tests, we assume we have the field objectMapper of type com.fasterxml.jackson.databind.ObjectMapper. Learn three ways to deserialize nested JSON values in Java using the Jackson library.