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 - This tutorial focuses on understanding the Jackson ObjectMapper class and how to serialize Java objects into JSON and deserialize JSON string into Java objects.
Discussions

shapefile - Convert Java object to JSON with jackson - Geographic Information Systems Stack Exchange
I am trying to read the data from a shapefile and once read, I want to convert it to a JSON file. To read the data I'm using the geoTools library, and to write to JSON, I use the geojson-jackson l... More on gis.stackexchange.com
🌐 gis.stackexchange.com
June 11, 2019
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
May 1, 2020
From JSON to Java Objects with Jackson | Java Beginners

Jackson databind is a terrible idea, good lord the amount of shit vulnerabilities we have to clean up every release because of this piece of junk is unreal.

More on reddit.com
🌐 r/programming
1
0
October 6, 2021
How to convert object to json in controller?
Spring will do it for you. Just create a separate controller and annotate the class with @RestController. Then just return your object or list of objects in your controller method. Spring will automatically use Jackson to serialize it to JSON. 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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-java-object-to-json-string-using-jackson-api
Convert Java Object to Json String using Jackson API - GeeksforGeeks
3 weeks ago - 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; } }
🌐
Quora
quora.com › How-can-I-parse-String-to-JSON-using-Jackson
How to parse String to JSON using Jackson - Quora
Answer (1 of 2): Assuming your json string represents a student as this : [code]{ "name": "Student 1", "age": 19, "scores": [10.0, 11.1, 12.2] } [/code]You can use Jackson’s ObjectMapper as this (assuming your json string is referenced by [code ]studentJson[/code]) : [code]final ObjectMapper...
🌐
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.
Find elsewhere
Top answer
1 of 1
2

Why not use GeoTools to handle the input and output? Then your code becomes simply something like:

File inFile = new File("/home/ian/Data/states/states.shp");
File outFile = new File("/home/ian/Data/states/states.geojson");
if (outFile.exists()) {
  outFile.delete();
}
outFile.createNewFile();
// Read
DataStore inputDataStore = DataStoreFinder.getDataStore(
        Collections.singletonMap("url", URLs.fileToUrl(inFile)));

String inputTypeName = inputDataStore.getTypeNames()[0];
SimpleFeatureType inputType = inputDataStore.getSchema(inputTypeName);

FeatureSource<SimpleFeatureType, SimpleFeature>
        source = inputDataStore.getFeatureSource(inputTypeName);

FeatureCollection<SimpleFeatureType, SimpleFeature>
        inputFeatureCollection = source.getFeatures();

Map<String, Object> params = new HashMap<>();
params.put(GeoJSONDataStoreFactory.URLP.key, URLs.fileToUrl(outFile));
// Write
DataStore newDataStore = DataStoreFinder.getDataStore(params);

newDataStore.createSchema(inputType);
String typeName = newDataStore.getTypeNames()[0];

SimpleFeatureStore featureStore =
        (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);

featureStore.addFeatures(inputFeatureCollection);
newDataStore.dispose();
inputDataStore.dispose();

}

If you don't want to export all the attributes of the shapefile features you need to modify newDataStore.createSchema(inputType); to use a new limited schema. This needs something like:

SimpleFeatureType outType = DataUtilities.createSubType(inputType, new String[] {"type","REFCAT"});
newDataStore.createSchema(outType);
🌐
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.
🌐
Codemia
codemia.io › knowledge-hub › path › converting_java_objects_to_json_with_jackson
Converting Java objects to JSON with Jackson
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
JSON Formatter
jsonformatter.org › json-parser
JSON Parser Online to parse JSON
Ad blocking? It's okay. Please share to support us: ... Online JSON Parser helps to parse, view, analyze JSON data in Tree View.
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-objectmapper.html
Jackson ObjectMapper
The Jackson ObjectMapper can read JSON into Java objects and write Java objects to JSON. This Jackson ObjectMapper tutorial explains how to use the Jackson ObjectMapper class.
🌐
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.
🌐
Maven Repository
mvnrepository.com › artifact › com.fasterxml.jackson.core › jackson-databind
Maven Repository: com.fasterxml.jackson.core » jackson-databind
2 weeks ago - Jackson Core is a core library for Jackson that provides low-level streaming JSON input/output functionality. ... aar android apache api arm assets build build-system bundle client clojure cloud config cran data database eclipse example extension framework github gradle groovy io ios javascript jvm kotlin library logging maven mobile module npm osgi persistence plugin resources rlang sdk server service spring sql starter testing tools ui web webapp
🌐
Mkyong
mkyong.com › home › java › write json to a file with jackson
Write JSON to a file with Jackson - Mkyong.com
April 25, 2024 - Parse JSON Array with Jackson · Convert Java Objects to JSON with Jackson · Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities. 0 Comments · Most Voted · Newest Oldest ·
🌐
Twilio
twilio.com › en-us › blog › developers › tutorials › building-blocks › java-json-with-jackson
Three ways to use Jackson for JSON in Java | Twilio
June 13, 2025 - This is common in JSON documents as it avoids precision issues on very small or very large numbers. Jackson allows you to read JSON into a tree model: Java objects that represent JSON objects, arrays and values.
🌐
Medium
medium.com › @bubu.tripathy › json-serialization-and-deserialization-in-java-2a3f08266b70
JSON Serialization and Deserialization in Java | by Bubu Tripathy | Medium
April 9, 2023 - When we serialize the `Employee` object to JSON, the `Address` properties will be flattened into the JSON output instead of being nested under an `address` property. Now let’s take a look at an example of using the `@JsonSetter` and `@JsonGetter` annotations to customize the setter and getter methods used during serialization and deserialization: import com.fasterxml.jackson.annotation.JsonGetter; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.ObjectMapper; public class Example { public static void main(String[] args) throws Exception { ObjectMapper
🌐
Reddit
reddit.com › r/javahelp › convert java object to json without external library
r/javahelp on Reddit: Convert Java object to JSON without external library
May 1, 2020 -

I feel like an idiot, I mean, I have been programming for a few years, mostly C# and Java, but all the REST apis I made was built with .NET, so I had no need for it.

Well, I can't find how to convert a Java object to a JSON WITHOUT the need of an external library, anyone?

🌐
GitHub
github.com › FasterXML › jackson
GitHub - FasterXML/jackson: Main Portal page for the Jackson project · GitHub
These extensions are plug-in Jackson Modules (registered with ObjectMapper.registerModule()), and add support for datatypes of various commonly used Java libraries, by adding serializers and deserializers so that Jackson databind package (ObjectMapper / ObjectReader / ObjectWriter) can read and write these types. Datatype modules directly maintained by Jackson team are under the following Github repositories: ... jackson-module-parameter-names: Module that adds support for using a new JDK8 feature, ability to access names of constructor and method parameters, to allow omitting @JsonProperty.
Starred by 9.7K users
Forked by 1.2K users
🌐
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.