How do I parse JSON in JavaScript?
Advanced Topics
In JavaScript you parse JSON with the built-in JSON.parse(), which turns a JSON string into a JavaScript object. To go the other way, JSON.stringify() converts an object back into a JSON string. No library is needed.
// String -> object
const obj = JSON.parse('{ "name": "Ada", "age": 36 }');
console.log(obj.name); // "Ada"
// Object -> string
const text = JSON.stringify(obj);
// Pretty-printed with 2-space indent
const pretty = JSON.stringify(obj, null, 2);
Handle invalid JSON safely
JSON.parse throws a SyntaxError on malformed input, so wrap it in a try/catch:
try {
const data = JSON.parse(input);
} catch (err) {
console.error("Invalid JSON:", err.message);
}
Fetching JSON from an API
const res = await fetch("/api/users");
const data = await res.json(); // parses automatically
Before parsing, make sure the JSON is valid by pasting it into the validator. Reading data in another language? See JSON in Python.
Still Have Questions?
Check out our other FAQ topics or return to the JSON Copilot app