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
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);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);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
Use JSON.stringify(value, null, 2). The third argument sets the indentation; pass a number of spaces or a string like '\t' for tabs.
Parse it first with JSON.parse(), then pass the result to JSON.stringify() with indentation: JSON.stringify(JSON.parse(str), null, 2).
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.
JSON Formatter
Format and beautify JSON with proper indentation.
JSON Validator
Validate JSON syntax with detailed error messages.
JSON Minifier
Compress JSON by removing whitespace to reduce size.
JSON Beautifier
Turn minified JSON into clean, readable code.
CSV to JSON
Convert CSV data into JSON for APIs and apps.
Learn More About JSON
All JSON syntax rules and data types on one page.
View cheat sheetFix the 10 most frequent JSON syntax mistakes.
Read guide