There is a built-in method to convert a JSONObject to a String. Why don't you use that:

JSONObject json = new JSONObject();

json.toString();
Answer from Tanu Garg on Stack Overflow
Discussions

Converting Java objects to JSON with Jackson - Stack Overflow
Alternatively, you can also use cjson library for converting java object to json string - maven More on stackoverflow.com
🌐 stackoverflow.com
arrays - How to convert jsonString to JSONObject in Java - Stack Overflow
In that case I'd have a class that will correspond to the properties of your JSON Object · Copyclass Phone { public String phonetype; public String cat; } ... More on stackoverflow.com
🌐 stackoverflow.com
Converting a large java object to String containing json format
this refers to a large java object,I get out of memory exception while converting,also our heap size is 1 GB.Any workaround for this ? More on github.com
🌐 github.com
5
October 23, 2017
Converting Json to Java object
Jackson is a library that people use to deserialize json to Java. More on reddit.com
🌐 r/javahelp
10
7
December 13, 2022
🌐
Baeldung
baeldung.com › home › json › jackson › intro to the jackson objectmapper
Intro to the Jackson ObjectMapper - JSON
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:
🌐
Tabnine
tabnine.com › home › convert java object to json
Convert Java object to JSON - Tabnine
July 25, 2024 - Converting a Java Obj to a JSON string is simple using JACKSON or GSON API. In our examples, we provided the code to make it easy for you to reproduce in your IDE. ... Create a new project (Maven is recommended).
🌐
freeCodeCamp
freecodecamp.org › news › jsonobject-tostring-how-to-convert-json-to-a-string-in-java
JSONObject.toString() – How to Convert JSON to a String in Java
April 14, 2023 - This string representation is formatted according to the JSON syntax and can be used to transmit the JSON object over a network, store it in a file, or display it on a web page.
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"}

Find elsewhere
🌐
Medium
medium.com › @bectorhimanshu › parsing-a-string-into-a-jsonobject-in-java-and-vice-versa-1589b0b9edd2
Parsing a String into a JSONObject in Java and vice-versa | by bectorhimanshu | Medium
September 29, 2023 - In this blog, I will provide examples for both converting a JSONObject to a String and parsing a String into a JSONObject using the org.json.JSONObject class. a. To create a new JSONObject with desired properties in Java, we will need to use a JSON library. In this example, we’ll use the ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-java-object-to-json-string-using-gson
Convert Java Object to Json String using GSON - GeeksforGeeks
July 11, 2025 - GSON: It is an open-source Java library which is used to serialize and deserialize Java objects to JSON.
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
How to Convert a Java Object into a JSON String - Java Code Geeks
August 19, 2019 - Converting a Java Obj to JSON string is simple using JACKSON or GSON API. In our examples we provided the code to make it easy for you to reproduce in your IDE. ... Include the JAR files into your classpath by adding dependencies to the pom file.
🌐
W3Docs
w3docs.com › java
Convert JsonObject to String | W3Docs
import com.google.gson.Gson; import com.google.gson.JsonObject; JsonObject obj = new JsonObject(); obj.addProperty("key", "value"); Gson gson = new Gson(); String jsonString = gson.toJson(obj);
🌐
Baeldung
baeldung.com › home › json › convert string to jsonobject with gson
Convert String to JsonObject with Gson | Baeldung
May 5, 2025 - Learn a couple of methods for converting a JSON String into a JsonObject using the Gson library in Java.
🌐
Educative
educative.io › answers › how-to-convert-a-java-object-to-json
How to convert a Java object to JSON
The Google GSON​ library is the most popular library used for converting Java objects to JSON. The code to get a JSON String out of an object is given below.
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-json-string-to-java-object-using-gson
Convert Json String to Java Object Using GSON - GeeksforGeeks
July 11, 2025 - GSON: It is an open-source Java library which is used to serialize and deserialize Java objects to JSON. ... In this article, a predefined JSON String is converted into Java Object using GSON.
🌐
GitHub
github.com › google › gson › issues › 1176
Converting a large java object to String containing json format · Issue #1176 · google/gson
October 23, 2017 - Gson gson = null; GsonBuilder gsonBuilder = new GsonBuilder(); gson = gsonBuilder.serializeNulls().create(); return gson.toJson(this); this refers to a large java object,I get out of memory exception while converting,also our heap size i...
Author   google
🌐
Squash
squash.io › how-to-convert-java-objects-to-json-with-jackson
How To Convert Java Objects To JSON With Jackson
November 8, 2023 - The ObjectMapper class in the Jackson library provides methods to convert Java objects to JSON and vice versa. To convert a Java object to JSON, you can use the writeValueAsString() method of the ObjectMapper class. This method serializes the ...
🌐
JavaMadeSoEasy
javamadesoeasy.com › 2018 › 09 › convert-java-object-to-json-string-and.html
JavaMadeSoEasy.com (JMSE): Convert java object to JSON string and pretty print Json in java - using Jackson
Convert JSON string to java Object - using Jackson · Convert java object to JSON string and pretty print Json in java - using Jackson · convert JSON string to java Map - using Jackson · convert java Map to JSON string · How to rename JSON properties in java- Jackson api ·
🌐
Initial Commit
initialcommit.com › blog › convert-java-object-to-json
Convert Java object to JSON - Initial Commit
public static String ... Student object to Json"); ex.printStackTrace(); } return studentJson; } P.S: writeValueAsString() method accepts also Hashmap, ArrayList and Arrays....