JSON (JavaScript Object Notation) is a text format for structured data. In JavaScript you turn a JSON string into a value with JSON.parse() and turn a value into a JSON string with JSON.stringify(). Both live on the built-in JSON object, so no import is needed in browsers, Node.js, Deno or Bun.
JSON looks like a JavaScript object literal, but it is stricter: it is only text, it has a small fixed set of types, and it is the standard way to send data between a server and a web app with fetch().
JSON.parse()
JSON.stringify()
A JSON document is one value. That value is an object, an array, a string, a number, true, false or null.
The rules that most often trip people up:
{ name: "Asha" } is valid JavaScript but invalid JSON.undefined, no functions.NaN, Infinity and leading zeros such as 012 are not valid.application/json.JSON.parse()JSON.parse(text) reads a JSON string and returns the matching JavaScript value. Objects become plain objects, arrays become arrays, and null stays null.
If the text is not valid JSON, JSON.parse() throws a SyntaxError. Always wrap parsing of untrusted or external input in try...catch.
The optional second argument is a reviver function. It is called for every key and value, from the innermost values outward, and its return value replaces the parsed value. JSON has no date type, so a reviver is a common way to restore dates.
JSON.stringify()JSON.stringify(value) converts a JavaScript value into a JSON string.
Notice what happened: the note property with value undefined was dropped, and the Date became an ISO string because Date has a toJSON() method. JSON.stringify() is also lossy in these cases:
| Input value | Result in JSON |
|---|---|
undefined, functions, symbols in an object | Property is omitted |
undefined, functions, symbols in an array | Becomes null |
NaN, Infinity, -Infinity | Becomes null |
Map, Set | Becomes {} |
BigInt | Throws a TypeError |
| Circular reference | Throws a TypeError |
The second argument is a replacer (a function or an array of allowed keys) and the third is the indentation.
To control how a custom class is serialized, give it a toJSON() method. JSON.stringify() calls it and serializes the result instead.
In the browser, Response.json() reads the body and parses it in one step. Send JSON by stringifying the body and setting the Content-Type header.
In Node.js you can read a JSON file with JSON.parse(await readFile(path, "utf8")) using node:fs/promises. ES modules also support JSON imports with an import attribute: import config from "./config.json" with { type: "json" };.
eval() to parse JSON. JSON.parse() is safe because it only accepts data, never code.JSON.parse(JSON.stringify(x)) unless you accept the losses in the table above. Use structuredClone(x) instead, which keeps dates, maps, sets and handles circular references.JSON.parse() only checks syntax, not that fields exist or have the right type. Use a schema library or manual checks on input you do not control.Number.MAX_SAFE_INTEGER lose precision when parsed. Send them as strings.No. A JavaScript object is an in-memory value that can hold functions, symbols, dates and undefined. JSON is a text format with a strict subset of those types.
JSON.parse() throw "Unexpected token"?The text is not valid JSON. Typical causes are single quotes, unquoted keys, trailing commas, an empty string, or an HTML error page returned instead of JSON.
Store it as an ISO 8601 string, which is what Date.prototype.toJSON() produces, and convert it back with a reviver or new Date(value).
Pass an indent as the third argument: JSON.stringify(value, null, 2).
JSON is a strict text format built on objects, arrays and a few primitive types. Use JSON.parse() to read it, JSON.stringify() to write it, handle SyntaxError for untrusted input, and remember that dates, undefined, Map and BigInt do not round-trip without extra work. Use structuredClone() for deep copies and validate parsed data before trusting it.