Photo by Eric Er on Unsplash

On this page

The global JSON object (ES5) serializes JavaScript values to text and parses JSON text back into values. It is not a general persistence format for functions, undefined, or circular graphs.

Parse and stringify

const obj = { name: "Ada", tags: ["math", "cs"] };
const text = JSON.stringify(obj);
// '{"name":"Ada","tags":["math","cs"]}'
JSON.parse(text); // plain object, not a class instance

Common stringify options:

JSON.stringify(obj, null, 2); // pretty print
JSON.stringify(obj, ["name"]); // allow-list keys
JSON.stringify(obj, (key, value) =>
key === "secret" ? undefined : value
);

Replacer returning undefined omits a property (same for array holes when filtering elements).

What JSON cannot represent

JavaScript In JSON
undefined, functions, symbols Omitted or null in arrays
NaN, Infinity Invalid—stringify uses null in arrays
Date Becomes ISO string unless you customize
Map, Set, typed arrays Plain objects/arrays only unless you transform
Circular references stringify throws

Use a reviver on parse to revive dates or typed values:

JSON.parse('{"at":"2026-01-01T00:00:00.000Z"}', (key, value) => {
if (key === "at" && typeof value === "string") return new Date(value);
return value;
});

Safe parsing

Never pass untrusted text to JSON.parse and then use eval or dynamic code paths on the result. For schema validation at boundaries, use a validator in your stack (TypeScript types alone do not validate runtime input).

In Node and browsers, fetch().json() and response.json() call JSON.parse on the body after UTF-8 decoding.

JSON in the shell

When logs, curl, or CLI tools emit JSON, JavaScript is not in the pipe—jq filters and reshapes documents in Bash. See Mastering JSON in Bash: A practical guide to jq for filters, paths, and composition with other commands.

Modules and import attributes (ES2025)

Some runtimes support importing JSON as a module:

import config from "./config.json" with { type: "json" };

Syntax and bundler support vary; verify your build target. For config at runtime, reading a file and JSON.parse remains the portable default in Node.

Continue with Regular expressions in JavaScript.

Search articles

Type a word to search published articles.

Open the search page