JSON.stringify(JSONObject) is a Javascript function and will not be available in Java. If you're using the org.json.* package built in the Android SDK, the equivalent would be to simply call toString() on your JSONObject instance, or the more human-friendly toString(int).

http://developer.android.com/reference/org/json/JSONObject.html#toString() http://developer.android.com/reference/org/json/JSONObject.html#toString(int)

CopyJSONObject obj = ...
String jsonString = obj.toString(4);
Answer from MH. on Stack Overflow
Top answer
1 of 4
33

JSON.stringify(JSONObject) is a Javascript function and will not be available in Java. If you're using the org.json.* package built in the Android SDK, the equivalent would be to simply call toString() on your JSONObject instance, or the more human-friendly toString(int).

http://developer.android.com/reference/org/json/JSONObject.html#toString() http://developer.android.com/reference/org/json/JSONObject.html#toString(int)

CopyJSONObject obj = ...
String jsonString = obj.toString(4);
2 of 4
16

I know this is old, but I ran into the same problem. And there doesn't seem to be much about it here... 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:

Copy import org.codehaus.jackson.JsonGenerationException;
 import org.codehaus.jackson.map.JsonMappingException;
 import org.codehaus.jackson.map.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

CopyUser 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 various errors
  catch (JsonGenerationException e) {
    e.printStackTrace();
} 
  catch (JsonMappingException e) {
    e.printStackTrace();
}

The result should looks like this: {"firstName":"Sample","lastName":"User","email":"sampleU@example.com"}

🌐
Medium
medium.com › @LearnITbyPrashant › json-stringify-or-stringify-object-jsonobject-e55661fa5d63
JSON.stringify or stringify(Object jsonObject) | by Prashant Kumar LearnIT | Medium
January 9, 2024 - Sign in · Prashant Kumar LearnIT · 2 min read · ·Jan 9, 2024 · -- Listen · Share · Explanation:- Creates a string from a JSON object. The JSON.stringify() method can only convert numbers, strings, and Java native objects to strings.
🌐
GitHub
gist.github.com › RepComm › 72ad653df238ebe67b7734e2188a39d8
JSON stringify / parse in java · GitHub
JSON stringify / parse in java. GitHub Gist: instantly share code, notes, and snippets.
🌐
ServiceNow Community
servicenow.com › community › developer-blog › servicenow-things-to-know-77-json-stringify-or-stringify-object › ba-p › 2783663
ServiceNow Things to Know 77: JSON.stringify() or ... - ServiceNow Community
January 8, 2024 - The JSON.stringify() method can only convert numbers, strings, and Java native objects to strings. It cannot convert user-defined objects to strings, unless those objects provide a toJSON() method. JSON.stringify() converts a value to JSON notation using the following guidelines: If the value ...
🌐
Tabnine
tabnine.com › home page › code › java › leap.lang.json.json
leap.lang.json.JSON.stringify java code examples | Tabnine
*/ default THttpRequest setJson(Object value) { setContentType(ContentTypes.APPLICATION_JSON_UTF8); return setBody(JSON.stringify(value)); } ... protected void output(int status, Object result) { Output out = new Output(); out.status = status; out.result = result; writer.println(OUTPUT_PREFIX + JSON.stringify(out) + OUTPUT_SUFFIX); }
🌐
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 - The JSONObject.toString() method is a powerful tool that Java developers can use to convert JSON objects to strings.
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 497191 › equivalent-of-json-stringify-in-java
Equivalent of JSON.stringify() in JAVA | DaniWeb
function handleCSV(evt) { var file ... }; reader.readAsText(file); btnGenerate.disabled = false; } ... There is no built-in Java equivalent of JSON.stringify....
🌐
Coderanch
coderanch.com › t › 678890 › java › convert-string-Json-stringify-Json
How to convert string from Json.stringify back to Json or iterable object Java? (Java in General forum at Coderanch)
That string is not valid JSON. If you want multiple objects, it should be enclosed in angle brackets: You can then parse it as a JSON array, instead of a JSON object.
Find elsewhere
🌐
Narkive
nashorn-dev.openjdk.java.narkive.com › Ep5bnGQ4 › json-stringify-on-java-object
JSON.stringify on Java object
The fact it can't be extended by Nashorn internally to stringify POJOs comes from the sentence at end of 15.12.2, saying "It is not permitted for a conforming implementation of JSON.parse to extend the JSON grammars. If an implementation wishes to support a modified or extended JSON interchange format it must do so by defining a different parse function." Bummer… var b = new java.util.BitSet() b.set(3) b.set(5) print(JSON.stringify(b)) print(JSON.stringify({ "b": b, "a": "hello" })) // Let's define a replacer function function replaceBitSet(key, value) { if(value instanceof java.util.BitSet) { return { __kind__: "BitSet", setBits: getSetBits(value) } } return value; } // When given a BitSet, returns an Array containing indices of set bits // in increasing order.
🌐
W3Schools
w3schools.com › js › js_json_stringify.asp
JSON.stringify()
JSON.stringify() can not only convert objects and arrays into JSON strings, it can convert any JavaScript value into a string.
🌐
Apache
lists.apache.org › thread › n2pwf48my3046mpqbyvjp9vxzyxk3hdx
Re: Using JSON.stringify() in JSR223 preprocessor ...
Email display mode: · Modern rendering · Legacy rendering · This site requires JavaScript enabled. Please enable it
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - Learn the fundamentals of parsing and manipulating JSON data with the JSON-Java library.
🌐
JSON Formatter
jsonformatter.org › json-stringify-online
JSON Stringify Online using JSON.Stringify()
JSON Stringify Online helps convert string value to JSON String using JSON.Stringify().
🌐
Attacomsian
attacomsian.com › blog › json-parse-stringify
Understanding JSON.parse() and JSON.stringify()
October 3, 2022 - As name suggests, the JSON.stringify() function transforms a JavaScript object into a JSON string. While sending JSON data from a client to a server, it must be converted into a JSON string.
🌐
Qvera
qvera.com › kb › index.php › 1156 › json-stringify-throws-an-exception
JSON.stringify() throws an exception - Knowledge Base - Qvera
I am building a JavaScript Object from messageCache, but when I use JSON.stringify(), it throws this exception: · EvaluatorException: Java class "[Ljava.lang.reflect.Constructor;" has no public instance field or method named "toJSON"
🌐
RestfulAPI
restfulapi.net › home › json › json stringify()
JSON stringify()
November 3, 2023 - The JSON.stringify() function, ... a JavaScript value to a serialized JSON string. JSON.stringify() can optionally use a replacer function to replace values using custom logic. ... replacer (optional) – The function that alters the behavior ...
🌐
javaspring
javaspring.net › blog › java-json-stringify
Java JSON Stringify: A Comprehensive Guide — javaspring.net
JSON stringify is the process of converting a Java object into a JSON-formatted string. A JSON string represents the data structure and values of the Java object in a text-based format that can be easily transmitted over the network or stored ...
🌐
Tabnine
tabnine.com › home › convert java object to json
Convert Java object to JSON - Tabnine
July 25, 2024 - The example below demonstrates a client-server scenario where the RESTful Web Service accepts data in XML/JSON: The RESTful web server app is designed using Java.
🌐
Baeldung
baeldung.com › home › json › escape json string in java
Escape JSON String in Java | Baeldung
January 8, 2024 - JSONObject jsonObject = new JSONObject(); jsonObject.put("message", "Hello \"World\""); String payload = jsonObject.toString(); This will take the quotes around “World” and escape them: ... One of the most popular and versatile Java libraries for JSON processing is Jackson.