"String to JSON" means parsing a text string that contains JSON syntax into an actual JSON object you can work with. Whether you need to parse a raw API response string, decode a minified JSON payload, or convert a JavaScript object back to JSON — here are the best free methods.
Method 1: Online JSON Formatter (No Code Needed)
If you have a raw JSON string and want to see it as a formatted, readable object:
- Open TextWonder's JSON Formatter.
- Paste your JSON string into the input box.
- Click Format. The tool parses the string and outputs a beautifully indented JSON object.
- If the string is invalid JSON, the tool highlights the syntax error.
Method 2: JSON.parse() in JavaScript
In JavaScript, convert a JSON string to an object with:
const jsonString = '{"name":"Alice","age":30}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // "Alice"
Always wrap JSON.parse() in a try/catch to handle invalid strings:
try {
const obj = JSON.parse(jsonString);
// use obj here
} catch (e) {
console.error('Invalid JSON:', e.message);
} Method 3: Object to JSON String (JSON.stringify)
To convert a JavaScript object to a JSON string:
const obj = { name: "Alice", age: 30 };
const jsonString = JSON.stringify(obj, null, 2);
// Result: '{\n "name": "Alice",\n "age": 30\n}' null, 2 adds 2-space indentation. Use null, 0 (or just JSON.stringify(obj)) for compact/minified output.
Method 4: Python — json.loads() and json.dumps()
In Python:
import json
# String → Python dict (equivalent to JSON object)
data = json.loads('{"name": "Alice", "age": 30}')
print(data["name"]) # Alice
# Python dict → JSON string
json_string = json.dumps(data, indent=2)
print(json_string) Why Is My JSON String Invalid?
Common reasons JSON.parse() fails:
- Single quotes: JSON requires double quotes —
'name'is invalid,"name"is correct. - Trailing commas:
[1, 2, 3,]is invalid JSON (trailing comma after last item). - Unquoted keys:
{name: "Alice"}is invalid — keys must be quoted:{"name": "Alice"}. - Double-escaped backslashes: Strings received from APIs may be double-encoded. Use the JSON Formatter to spot the issue.
Frequently Asked Questions
How do I convert a string to JSON?
In JavaScript, use JSON.parse(yourString) to convert a JSON string to a JavaScript object. To do it online, paste the string into TextWonder's JSON Formatter — it parses the string and shows the formatted JSON object.
What is a JSON string vs a JSON object?
A JSON string is text that looks like JSON but is stored as a string variable (e.g. '{"name":"Alice"}'). A JSON object is the parsed JavaScript value you can work with. JSON.parse() converts the string to the object.
How do I convert a JavaScript object to JSON?
Use JSON.stringify(yourObject) in JavaScript. This converts a JavaScript object to a JSON string. For example, JSON.stringify({name:"Alice",age:30}) returns '{"name":"Alice","age":30}'.
Why does JSON.parse() throw an error?
Common reasons: the string is not valid JSON (missing quotes around keys, trailing commas, single quotes instead of double quotes), or the string contains escaped characters that need unescaping first. Use TextWonder's JSON Formatter to spot the syntax error.
How do I convert an object to JSON string in Python?
Use import json and json.dumps(your_dict) to convert a Python dict to a JSON string. Use json.loads(your_string) to parse a JSON string back to a dict.