You are passing into the JSONObject constructor an instance of a StringBuilder class.

This is using the JSONObject(Object) constructor, not the JSONObject(String) one.

Your code should be:

JSONObject jsonObj = new JSONObject(jsonString.toString());
Answer from APD on Stack Overflow
🌐
Dadroit
dadroit.com › string-to-json
Online String to JSON Converter
JSON is a lightweight format for data exchange, easily readable by both humans and machines. Primarily used in web APIs and server-web application communication, it is language-independent and a popular alternative to XML for data transmission and configuration files. To convert String to JSON, visit the tool address, input your String data —or load your String file— and the tool will display the corresponding JSON output in real time.
🌐
JSON Formatter
jsonformatter.org › string-to-json-converter
String to JSON Converter online to convert JSON string to JSON
String to JSON Converter online converts JSON String to JSON data by removing escapped data.
People also ask

Can I convert JSON back to a string using this tool?
The primary function of this tool is to convert strings to JSON. However, you can use JSON Stringify to reverse the process and convert JSON back to a string.
🌐
testmu.ai
testmu.ai › home › free tools › string to json
Convert String to JSON Online | TestMu AI
How accurate is the String to JSON conversion?
The tool's accuracy is high, and it can convert most strings into JSON format. However, the conversion's success depends on the input string's structure and content.
🌐
testmu.ai
testmu.ai › home › free tools › string to json
Convert String to JSON Online | TestMu AI
Can I use the String to JSON Converter on any device?
Yes, the web-hosted version includes the String to JSON Converter, which means that you can use the tool from any web-enabled device that has, at least, a screen and a web browser. Namely, from a desktop, laptop, or tablet, through a smartphone—compatibility issues with the tool will be the least of your worries.
🌐
testmu.ai
testmu.ai › home › free tools › string to json
Convert String to JSON Online | TestMu AI
🌐
freeCodeCamp
freecodecamp.org › news › python-json-how-to-convert-a-string-to-json
Python JSON – How to Convert a String to JSON
November 9, 2021 - This process is called deserialization – the act of converting a string to an object. #include json library import json #json string data employee_string = '{"first_name": "Michael", "last_name": "Rodgers", "department": "Marketing"}' #check data type with type() method print(type(employee_string)) #convert string to object json_object = json.loads(employee_string) #check new data type print(type(json_object)) #output #<class 'dict'>
Top answer
1 of 6
40

You are passing into the JSONObject constructor an instance of a StringBuilder class.

This is using the JSONObject(Object) constructor, not the JSONObject(String) one.

Your code should be:

JSONObject jsonObj = new JSONObject(jsonString.toString());
2 of 6
5

@Nishit, JSONObject does not natively understand how to parse through a StringBuilder; instead you appear to be using the JSONObject(java.lang.Object bean) constructor to create the JSONObject, however passing it a StringBuilder.

See this link for more information on that particular constructor.

http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.Object%29

When a constructor calls for a java.lang.Object class, more than likely it's really telling you that you're expected to create your own class (since all Classes ultimately extend java.lang.Object) and that it will interface with that class in a specific way, albeit normally it will call for an interface instead (hence the name) OR it can accept any class and interface with it "abstractly" such as calling .toString() on it. Bottom line, you typically can't just pass it any class and expect it to work.

At any rate, this particular constructor is explained as such:

Construct a JSONObject from an Object using bean getters. It reflects on all of the public methods of the object. For each of the methods with no parameters and a name starting with "get" or "is" followed by an uppercase letter, the method is invoked, and a key and the value returned from the getter method are put into the new JSONObject. The key is formed by removing the "get" or "is" prefix. If the second remaining character is not upper case, then the first character is converted to lower case. For example, if an object has a method named "getName", and if the result of calling object.getName() is "Larry Fine", then the JSONObject will contain "name": "Larry Fine".

So, what this means is that it's expecting you to create your own class that implements get or is methods (i.e.

public String getName() {...}

or

public boolean isValid() {...}

So, to solve your problem, if you really want that higher level of control and want to do some manipulation (e.g. modify some values, etc.) but still use StringBuilder to dynamically generate the code, you can create a class that extends the StringBuilder class so that you can use the append feature, but implement get/is methods to allow JSONObject to pull the data out of it, however this is likely not what you want/need and depending on the JSON, you might spend a lot of time and energy creating the private fields and get/is methods (or use an IDE to do it for you) or it might be all for naught if you don't necessarily know the breakdown of the JSON string.

So, you can very simply call toString() on the StringBuilder which will provide a String representation of the StringBuilder instance and passing that to the JSONObject constructor, such as below:

...
StringBuilder jsonString = new StringBuilder();
while((readAPIResponse = br.readLine()) != null){
    jsonString.append(readAPIResponse);
}
JSONObject jsonObj = new JSONObject(jsonString.toString());
...
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
Convert Strings to JSON Objects in JavaScript with eval() | Pluralsight
March 31, 2025 - String data can be easily converted to JSON using the stringify() function, and also it can be done using eval(), which accepts the JavaScript expression that you will learn about in this guide.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-convert-string-to-json-object
Convert String to JSON Object - Python - GeeksforGeeks
Let's explore different methods to do this efficiently. json.loads() method is the most commonly used function for parsing a JSON string and converting it into a Python dictionary.
Published   July 11, 2025
🌐
Analytics Vidhya
analyticsvidhya.com › home › ways to convert string to json object
Ways to Convert String to JSON Object
March 21, 2024 - Let’s compare the different methods discussed for converting a string to a JSON object in terms of simplicity and ease of use: The `json.loads()` method is the most straightforward and recommended method for converting a string to a JSON object.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-string-to-json-in-javascript
How to Convert String to JSON in JavaScript? - GeeksforGeeks
August 5, 2025 - In this approach, we are using JSON.parse() in JavaScript to convert a JSON-formatted string (str) into a JavaScript object (res).
🌐
Testmu
testmu.ai › home › free tools › string to json
Convert String to JSON Online | TestMu AI
Processing: Upon submission, the tool parses the input string, verifying its format against JSON syntax rules. Conversion: It should convert the string into a JSON object in case the format is proper and resolve any nested structures or arrays given in the string.
🌐
Jsononline
jsononline.net › string-to-json
String To JSON - Convert Strings To JSON Online
Apart from this, users can also upload a file stored on their device, or fetch string by entering a URL that contains it. After that, click the “Convert to JSON” button to start the process.
🌐
W3Schools
w3schools.com › js › js_json_parse.asp
JSON.parse()
Convert a string into a date, using the reviver function: const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}'; const obj = JSON.parse(text, function (key, value) { if (key == "birth") { return new Date(value); } else { return value; } }); document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth; Try it Yourself » · Functions are not allowed in JSON. If you need to include a function, write it as a string.
🌐
Code Beautify
codebeautify.org › string-to-json-online
String to JSON Online to convert JSON Text to JSON Tree.
If you have String and you want to convert to JSON, by using js stringify() function.
🌐
Oracle
docs.oracle.com › en › database › other-databases › nosql-database › 24.1 › sqlreferencefornosql › function-convert-string-json.html
Function to Convert String to JSON
May 2, 2024 - You can use the parse_json function to parse the string values in the JSON field. In the Book1 field above, the value is a JSON document stored as a string. You parse the complete string to convert it to a JSON object and then select the title field. In the Book2 field, the value of the doc ...
🌐
C# Corner
c-sharpcorner.com › article › convert-string-to-json-in-c-sharp
Convert string to JSON in C#
June 6, 2023 - You can convert a string to a JSON object in C# by using the JsonSerializer.Deserialize method from the System.Text.Json namespace.
🌐
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.
🌐
Make Community
community.make.com › questions
How to convert strings that look like JSON to JSON objects - Questions - Make Community
November 17, 2024 - Greetings All, I am running into a problem where my JSON objects cease being JSON objects and become strings when I do set variable and get variable in order to have them in scope in the final (top) path where I am building my final JSON payload which causes formatting issues (See the red lines): I thought I might be able to simply string everything into one long path to keep it simple, but one of the routers is really necessary as the ‘trip’ can have no stops, one stop or five stops AND I can...