Developer Guide
JSON Parsing in JavaScript
JSON.parse needs try/catch. Reviver and replacer customize conversion. Dates become strings, undefined is dropped, BigInt and circular refs throw. The formatter shows line and column.
JSON.parse and JSON.stringify are the language’s JSON codec. They implement the JSON grammar, not “whatever a JavaScript object can hold.” Trailing commas are invalid. undefined, functions, and circular references are not JSON. Date values become ISO strings on the way out and stay strings on the way in unless you revive them.
Always wrap JSON.parse in try/catch. A SyntaxError is normal input, not an exceptional crash. /tools/json-formatter runs JSON.parse / JSON.stringify in the browser, formats or minifies, can sort keys, and reports errors with a line and column — the same numbers engines put on SyntaxError. This page is JavaScript; Python’s json module is a sibling guide.
JSON.parse and try/catch
JSON.parse(text) returns arrays, objects, strings, numbers, booleans, or null. It throws SyntaxError on truncated input, single quotes, comments, and trailing commas. Catch that error, show the message, and do not touch a half-built object.
JSON.parse('{"ok":true}') yields { ok: true }. JSON.parse('{"a":1,}') throws: Expected double-quoted property name in JSON at position 7 (line 1 column 8) in current V8. Paste the same text into /tools/json-formatter to see the caret on that column without opening DevTools.
try {
const data = JSON.parse(text);
} catch (err) {
if (err instanceof SyntaxError) {
// err.message includes line and column in modern engines
} else {
throw err;
}
}reviver
The second argument to JSON.parse is a reviver (key, value) => unknown, called from the inside out, including the root with key === ''. Typical use: turn ISO date strings back into Date.
Returning undefined deletes that key. Do not use a reviver to “fix” trailing commas — the parse already failed.
const text = '{"when":"2026-11-13T00:00:00.000Z"}';
const row = JSON.parse(text, (key, value) => {
if (key === 'when' && typeof value === 'string') {
return new Date(value);
}
return value;
});
// row.when instanceof Date === truestringify: space and replacer
JSON.stringify(value, replacer, space) walks enumerable own properties. space as a number (commonly 2) pretty-prints; space as a string uses that indent. /tools/json-formatter format mode is that pretty-print; minify is JSON.stringify(value) with no space.
replacer can be an array of allowed keys or a function (key, value) => unknown. Returning undefined from a function replacer drops the property. Getters and toJSON (including Date) run during stringify.
JSON.stringify({ ok: true, n: 1 }, null, 2);
// '{\n "ok": true,\n "n": 1\n}'
JSON.stringify({ a: 1, b: 2 }, ['a']);
// '{"a":1}'Dates, undefined, BigInt, circular refs
JSON.stringify({ d: new Date('2026-11-13T00:00:00.000Z') }) is {"d":"2026-11-13T00:00:00.000Z"}. After parse, d is a string. Use a reviver if you need a Date again.
Object properties whose values are undefined or functions are omitted: JSON.stringify({ a: 1, b: undefined, c() {} }) is {"a":1}. In arrays those slots become null. JSON.stringify({ n: 1n }) throws TypeError: Do not know how to serialize a BigInt. A cycle throws TypeError: Converting circular structure to JSON. Convert BigInt to a string yourself; break cycles before you serialize.
Line and column errors in the formatter
/tools/json-formatter validates with JSON.parse. On failure it surfaces the engine’s line and column so you can jump to a trailing comma, a smart quote, or undefined written as a bare word. Sort keys is a stringify pass with sorted object keys — useful when a diff is noisy, not a JSON feature.
The tree view walks the parsed value. Edit in /tools/json-editor (Ace) for folding. Escape strings with /tools/json-escape. Nothing is uploaded.
Common pitfalls
Calling JSON.parse on a value that is already an object. Logging JSON.stringify(error) and wondering where the stack went (Error serializes as {}). Trailing commas, comments, and single quotes are JavaScript object literals, not JSON. NaN and Infinity become null. Sparse arrays stringify with null holes.
- Wrap every
JSON.parseof untrusted text intry/catch. Date→ string;undefined/ functions dropped; BigInt and cycles throw./tools/json-formatterreports line and column for the same grammar.
Worked examples
Trailing comma is not JSON
{"a":1,}SyntaxError: Expected double-quoted property name in JSON at position 7 (line 1 column 8)The same caret appears in /tools/json-formatter. Remove the comma.
Date stringifies to ISO
JSON.stringify({ d: new Date('2026-11-13T00:00:00.000Z') }){"d":"2026-11-13T00:00:00.000Z"}JSON.parse of that output gives a string, not a Date. Revive it if you need the object.
undefined properties disappear
JSON.stringify({ a: 1, b: undefined }){"a":1}The key b is gone, not null. Use null when the field must survive a round trip.
Frequently asked questions
Should I JSON.parse inside try/catch?
Yes for any string you did not just stringify yourself in the same tick. Malformed JSON is expected input.
Why did my Date become a string?
Date.prototype.toJSON emits ISO 8601. JSON has no date type. Parse with a reviver if you need Date instances.
Why does stringify throw on my object?
BigInt values and circular references throw. Functions and undefined on objects are dropped instead. Break the cycle or convert the BigInt.
Are trailing commas allowed?
No. JSON.parse rejects them. The formatter will point at the line and column. JSONC is a different format.
Does the formatter upload my document?
No. /tools/json-formatter and /tools/json-editor run JSON.parse / JSON.stringify in the tab.
Summary
Parse with try/catch, pretty-print with a space argument, and remember JSON is a subset of JavaScript values: no undefined, no functions, no cycles, no BigInt, dates as strings. When a payload fails, paste it into /tools/json-formatter and read the line and column before you blame the network.