To format JSON in the browser console, run JSON.stringify(value, null, 2). The third argument sets the indent, so you get two spaces per level. If what you have is a JSON string rather than an object, parse it first: JSON.stringify(JSON.parse(text), null, 2).
How to format JSON in the browser console with JSON.stringify
JSON.stringify formats JSON in any browser console when you pass a third argument called space. According to the JSON.stringify reference, a number means that many spaces per indent level, clamped to 10. Leave it out and no whitespace is added at all, which is why plain JSON.stringify(obj) prints one long line.
Open DevTools, switch to the Console tab, and type:
const order = { id: 7, items: [{ sku: "A1", qty: 2 }], paid: true };
JSON.stringify(order, null, 2);
The console prints the string with line breaks and two-space indents. Two details:
- Tabs work too. A string
spaceis inserted before every nested level, using at most its first 10 characters.JSON.stringify(order, null, "\t")gives tab indents. - The middle argument filters keys. The
replacercan be an array of property names to keep.JSON.stringify(order, ["id", "paid"], 2)dropsitems. Handy when the payload is huge and you need two fields.
If the console shows the result inside quotes with \n escapes, wrap it in console.log(...). Logging a string prints its contents rather than its literal form.
Format a JSON string versus a JavaScript object
A JSON string must be parsed before you can format it, because JSON.stringify on a string just wraps it in another layer of quotes. This is the most common mistake. You paste a response body in as a string, call JSON.stringify(text, null, 2), and get the same one-liner back, now escaped.
| What you have | What to run |
|---|---|
| A JavaScript object or array | JSON.stringify(value, null, 2) |
A JSON string (for example from localStorage) |
JSON.stringify(JSON.parse(text), null, 2) |
| A value you want to click through | Type the variable name, or console.dir(value) |
| An array of similar objects | console.table(value) |
localStorage.getItem("cart") always returns a string, so the line that formats it is JSON.stringify(JSON.parse(localStorage.getItem("cart")), null, 2).
If JSON.parse throws, the string isn't valid JSON. Look for trailing commas, single quotes or a truncated paste; the guide to unexpected token errors walks through each one.
Copy formatted JSON to the clipboard with copy()
The copy() helper puts text on your clipboard straight from the console, which saves selecting a long output by hand. In Chrome, the Console Utilities reference says copy(object) copies a string representation of the object. These helpers only work when typed into the Console, not inside your page's own scripts.
Format first, then copy the string:
copy(JSON.stringify(order, null, 2));
Firefox is why. The Firefox Web Console helpers page says strings are copied as-is, but any other value goes through JSON.stringify first, with no indent. So copy(order) gives you one compact line.
Two more shortcuts:
$_holds the last result. After a long expression,copy($_)copies its value without retyping it.- Right-click a string output. Chrome offers three copy options: Copy as JavaScript literal, Copy string contents and Copy as JSON literal.
Show arrays as a table or an expandable tree
console.table() turns an array of objects into a sortable grid, which often reads better than indented JSON. The console.table reference says each item becomes a row, with an index column, and an optional second argument limits the columns.
const users = [
{ id: 1, name: "Asha", role: "admin" },
{ id: 2, name: "Ben", role: "viewer" }
];
console.table(users, ["name", "role"]);
In Chrome you can then right-click the table and copy it as Markdown or CSV. In Firefox, console.table() stops at 1000 rows, including the heading row, so very long arrays get cut off.
For nested data, type the variable name and press Enter to get an expandable tree. console.dir(value) gives a plain property listing and, per the console.dir reference, doesn't try to pretty-print. Chrome shows an object's own properties first in bold. Getter values stay hidden until you click the (...) next to them.
Format a JSON response with Firefox's built-in viewer
Firefox formats JSON responses without any console code. The Firefox JSON viewer opens whenever you load a JSON file or a URL served as application/json. Objects and arrays start collapsed, a search box filters the tree, and a raw view lets you see and pretty-print the original text.
It also hands the data to the console. Open the Web Console on that tab: $json.data is the parsed object, $json.text the original response text, and $json.headers the request and response headers. copy(JSON.stringify($json.data, null, 2)) copies the whole response, formatted.
The viewer doesn't pretty-print JSON Lines files, where each line is its own document. Chrome has no built-in equivalent; see viewing JSON in Chrome for the options there.
Errors you'll hit when formatting real data
Two kinds of data make JSON.stringify throw instead of printing, and a few more get silently changed.
- BigInt values. Serializing one throws
TypeError: BigInt value can't be serialized in JSON. Convert first with a replacer:JSON.stringify(data, (k, v) => typeof v === "bigint" ? v.toString() : v, 2). The related problem of IDs losing digits is covered in large numbers in JSON.parse. - Circular references. A DOM node or an object that points back to its parent throws a TypeError, shown in Firefox as
TypeError: cyclic object value. Pick the fields you need with an array replacer, or see fixing circular structure errors. - Values that vanish.
undefined, functions and Symbols are dropped from objects and becomenullinside arrays. - Maps and Sets. Both come out as
{}. Spread them first:JSON.stringify([...myMap], null, 2).
If a key you expected is missing from the output, check for item 3 before blaming the API.
When the console is the wrong tool
The console is fine for one object, but awkward for a pasted payload of thousands of lines. Long output is hard to scroll, and a big blob pasted into one expression gets fiddly.
My rule: JSON.stringify(x, null, 2) for anything already in a variable, a separate page for large pasted payloads. The JSON formatter on this site runs in the browser, indents with 2 spaces by default, and lets you copy or download the result. It only formats and checks syntax. It won't validate against a JSON Schema. Outside the browser, pretty-printing JSON in Python does the same job from a terminal.
FAQ
How do I pretty print JSON in the Chrome console?
Run JSON.stringify(value, null, 2) on an object. If you have a JSON string, wrap it first: JSON.stringify(JSON.parse(text), null, 2).
How do I copy formatted JSON from the console?
Run copy(JSON.stringify(value, null, 2)). The copy() helper copies strings as they are, so the indentation survives. It only works when typed in the DevTools Console, not in page scripts.
What is the maximum indent JSON.stringify accepts?
A number indent is clamped to 10 spaces. A string indent uses at most its first 10 characters, so you can pass a tab character instead of spaces.
Why does JSON.stringify throw in the console?
The two common causes are a BigInt value and a circular reference. Both raise a TypeError instead of returning a string.
How do I view a JSON response as a table?
Call console.table(data) on an array of objects. Each item becomes a row, and you can pass a second argument to pick the columns.