JavaScript Guide

JSON Formatter for JavaScript

Learn how to format and pretty print JSON in JavaScript with JSON.stringify() — or paste your data into our free online formatter for a one-off job.

Format JSON in JavaScript: Code Examples

Pretty print with JSON.stringify()
const data = { name: "John", age: 30, roles: ["admin", "user"] };

// Third argument is the indentation (number of spaces)
const formatted = JSON.stringify(data, null, 2);
console.log(formatted);
Format a JSON string you received
const raw = '{"name":"John","age":30}';

// Parse first, then re-stringify with indentation
const pretty = JSON.stringify(JSON.parse(raw), null, 2);
console.log(pretty);
Filter which keys to include (replacer)
const user = { id: 1, name: "John", password: "secret" };

// Only keep id and name in the output
const safe = JSON.stringify(user, ["id", "name"], 2);
console.log(safe);

Prefer a quick online tool?

You don't need to write code to format JSON once. Paste it into our free formatter and get clean, indented output instantly — nothing leaves your browser.

JavaScript JSON FAQ

How do I pretty print JSON in JavaScript?

Use JSON.stringify(value, null, 2). The third argument sets the indentation; pass a number of spaces or a string like '\t' for tabs.

How do I format a JSON string instead of an object?

Parse it first with JSON.parse(), then pass the result to JSON.stringify() with indentation: JSON.stringify(JSON.parse(str), null, 2).

Can I exclude some fields when formatting?

Yes. Pass an array of allowed keys, or a replacer function, as the second argument to JSON.stringify() to control which fields appear.

Free JSON Tools You Can Use Right Now

Put these tips into practice with our free online JSON tools. No signup, everything runs in your browser.

Learn More About JSON

JSON Cheat Sheet

All JSON syntax rules and data types on one page.

View cheat sheet
Common JSON Errors

Fix the 10 most frequent JSON syntax mistakes.

Read guide