Pretty Print JSON in JavaScript: Browser and Node.js Examples

Last updated:

Pretty-printing JSON adds indentation and line breaks to make compact JSON human-readable. In JavaScript, JSON.stringify(value, null, 2) does it in 1 call — the 3rd argument sets indent width (0–10 spaces, or '\t' for tabs). In the terminal, python -m json.tool and jq . both format without installing anything. Passing null as the replacer argument preserves all fields; passing an array of key strings or a replacer function filters or transforms values. This guide covers browser and Node.js JSON.stringify usage, the space edge cases (values above 10 are clamped to 10), pretty-printing inside a <pre> HTML tag, terminal options (python -m json.tool, jq ., cat file.json | python3 -m json.tool), and the toJSON() override method.

Pretty print a JavaScript object

If you already have a JavaScript object, stringify it with a spacing argument. Two spaces is the safest default for docs, config examples, and debug output.

const data = {
  user: {
    id: 42,
    name: 'Alice'
  },
  roles: ['admin', 'editor']
}

const pretty = JSON.stringify(data, null, 2)
console.log(pretty)

Pretty print a JSON string

If the input is already a JSON string, parse it first, then stringify the parsed value.

const raw = '{"user":{"id":42,"name":"Alice"},"roles":["admin","editor"]}'
const pretty = JSON.stringify(JSON.parse(raw), null, 2)

console.log(pretty)

// {
//   "user": {
//     "id": 42,
//     "name": "Alice"
//   },
//   "roles": [
//     "admin",
//     "editor"
//   ]
// }

Handle invalid JSON safely

Pretty printing a string requires parsing, and parsing can throw. Wrap untrusted input intry/catch.

function prettyPrintJson(raw) {
  try {
    return {
      ok: true,
      value: JSON.stringify(JSON.parse(raw), null, 2)
    }
  } catch (error) {
    return {
      ok: false,
      error: error instanceof Error ? error.message : 'Invalid JSON'
    }
  }
}

Choose 2 spaces, 4 spaces, or tabs

The third argument accepts a number or a string. Numbers are clamped to 10 spaces. Strings are used as the indentation unit.

JSON.stringify(data, null, 2)   // 2 spaces
JSON.stringify(data, null, 4)   // 4 spaces
JSON.stringify(data, null, '	') // tabs

For shared examples, use 2 spaces unless your project has a strong convention.

Pretty print JSON in the browser

A small browser formatter can read a textarea, parse the JSON, and write formatted output.

const input = document.querySelector('#json-input')
const output = document.querySelector('#json-output')

try {
  output.textContent = JSON.stringify(JSON.parse(input.value), null, 2)
} catch (error) {
  output.textContent = 'Invalid JSON'
}

Pretty print JSON in Node.js

For scripts, parse stdin or file content, then output formatted JSON.

import { readFileSync } from 'node:fs'

const raw = readFileSync('data.json', 'utf8')
const pretty = JSON.stringify(JSON.parse(raw), null, 2)

console.log(pretty)

Common mistakes

  • Calling JSON.stringify(raw, null, 2) on a JSON string instead of parsing it first.
  • Expecting pretty printing to fix invalid JSON syntax.
  • Forgetting that functions, undefined, and symbols do not appear in JSON output.
  • Pretty printing large files in the UI thread without considering browser performance.

Pretty print JSON instantly

Use Jsonic's JSON Formatter when you want readable output, validation errors, minification, file upload, and copy/download actions in one place.

Open JSON Formatter

Frequently asked questions

How do I pretty print JSON with 2 spaces?

Call JSON.stringify(value, null, 2). The third argument is the indentation width. Two spaces is the most common convention for documentation, config files, and debug output.

What is the difference between pretty print and minify?

Pretty printing adds newlines and indentation for human readability, increasing file size. Minifying removes all whitespace for compact network payloads. JSON.stringify(value)minifies by default.

How do I pretty print JSON in Node.js?

Read the file with fs.readFileSync, parse it, then stringify with indentation:JSON.stringify(JSON.parse(raw), null, 2). Write back withfs.writeFileSync.

Can I pretty print JSON in the browser console?

The browser DevTools console automatically formats objects as an expandable tree. For the formatted string, run JSON.stringify(obj, null, 2) in the console.

What is the maximum indentation JSON.stringify supports?

The space argument is clamped to 10. Numbers above 10 are treated as 10. You can also pass a string like '\t' for tab indentation.

How do I pretty print JSON to a file in Node.js?

Use fs.writeFileSync('output.json', JSON.stringify(data, null, 2) + '\n'). The trailing newline keeps the file POSIX-compliant and avoids diff noise in version control.