JSON.parse() Online - Convert String to Object

Parse JSON strings into JavaScript objects. Test JSON.parse() behavior, debug parsing errors, and explore your data structure.

1
Paste your JSON string here to parse...
OutputEMPTY
1
 
Formatted output will appear here...

How JSON.parse() Works

// JSON string (what you have)
const jsonString = '{"name": "John", "age": 30}';

// Parse to JavaScript object (what you get)
const obj = JSON.parse(jsonString);

console.log(obj.name); // "John"
console.log(obj.age);  // 30

JSON.parse() converts a JSON-formatted string into a usable JavaScript object.

Common JSON.parse() Use Cases

Parsing API Responses

fetch('/api/user')
  .then(res => res.json())
  .then(data => {
    // data is already parsed
    console.log(data.name);
  });

Note: res.json() calls JSON.parse() internally.

Reading from localStorage

// Save object to localStorage
localStorage.setItem('user', 
  JSON.stringify(user));

// Read back from localStorage
const user = JSON.parse(
  localStorage.getItem('user')
);

Deep Cloning Objects

// Create a deep copy
const clone = JSON.parse(
  JSON.stringify(original)
);

// clone is independent of original

Note: This doesn't work with functions, undefined, or circular refs.

Parsing Config Files

// Reading package.json
const fs = require('fs');
const config = JSON.parse(
  fs.readFileSync('config.json')
);

console.log(config.version);

Understanding JSON.parse()

What is JSON.parse()?

JSON.parse() is a built-in JavaScript method that parses a JSON string and constructs the JavaScript value or object described by the string. It's the opposite of JSON.stringify(), which converts JavaScript objects to JSON strings.

Return Values

JSON.parse() can return any valid JavaScript value: an object, an array, a string, a number, a boolean, or null. The return type depends on the JSON structure. Objects are returned as Object instances, arrays as Array instances.

Error Handling

If the input string is not valid JSON, JSON.parse() throws a SyntaxError. Always wrap JSON.parse() in a try-catch block when parsing untrusted input to handle potential errors gracefully and prevent your application from crashing.

Reviver Function

JSON.parse() accepts an optional second parameter called a "reviver" function. This function is called for each key-value pair and allows you to transform values during parsing. It's useful for converting date strings to Date objects or performing custom transformations.

Security Considerations

JSON.parse() is safe to use with untrusted data because it only parses JSON. Unlike eval(), it won't execute arbitrary JavaScript code. Always use JSON.parse() instead of eval() when parsing JSON data from external sources.

Browser Support

JSON.parse() is supported in all modern browsers and has been available since IE8, Firefox 3.5, Chrome 3, and Safari 4. In Node.js, it's been available since the very first version. No polyfill is needed for modern development.

Frequently Asked Questions

What's the difference between JSON.parse() and JSON.stringify()?
JSON.parse() converts a JSON string → JavaScript object. JSON.stringify() does the opposite: JavaScript object → JSON string. They are complementary methods used together for serializing and deserializing data, such as when storing objects in localStorage or sending data in HTTP requests.
Why is JSON.parse() throwing "Unexpected token"?
This error means the input is not valid JSON. Common causes: single quotes instead of double quotes, trailing commas, unquoted keys, or extra characters before/after the JSON. Use our validator to find the exact location of the error.
Can JSON.parse() handle dates?
JSON doesn't have a native Date type, so dates are typically represented as strings (ISO 8601 format). JSON.parse() returns them as strings. Use a reviver function to convert date strings to Date objects: JSON.parse(str, (k, v) => k === 'date' ? new Date(v) : v)
Is JSON.parse() synchronous or asynchronous?
JSON.parse() is synchronous and blocking. For very large JSON strings (multi-megabyte), parsing can block the main thread. In performance-critical applications, consider parsing in a Web Worker or using streaming JSON parsers.

Related JSON Tools