How to Format JSON in JavaScript
Feb 06, 2026 3 min read
JavaScript has built-in support for JSON because it derives from standard JavaScript object syntax. Using JSON.stringify(), you can parse objects and arrays and instantly generate a beautifully formatted multiline string.
Example Code
JavaScript
const jsonString = '{"name":"John","age":30,"city":"New York"}';
const parsedData = JSON.parse(jsonString);
const formattedJson = JSON.stringify(parsedData, null, 2);
console.log(formattedJson); Common Use Cases
- Formatting responses from HTTP requests in Node.js endpoints
- Logging human-readable debug structures
- Presenting JSON blocks dynamically inside functional web apps
💡 Pro Tips for JavaScript
- The third argument indicates how many spaces to insert for indentation.
- The second argument (which is
nullhere) is a Replacer function to selectively filter out keys like user passwords before serialization.
Fastest JS Formatter
Why use <code>console.log</code> when you can see your data in a beautiful, interactive tree? Use our client-side tool for instant results.