DEV Community

Cover image for JavaScript built-in & beautify JSON
undqurek
undqurek

Posted on • Edited on • Originally published at dirask.com

5 3

JavaScript built-in & beautify JSON

Do you know the best way how to beautify JSON in pure JavaScript?
As answer: now I know?
But in the past it wasn't obvious.
I have used online tools.
Today, I know quick solution:

const beautifyJson = (json) => {
    var object = JSON.parse(json);
    return JSON.stringify(object, null, 4); // 4 spaces as indent
}

// Usage example:

const uglyJson = '{"id": 2, "name": "Tom", "age": 25}';
const beautyJson = beautifyJson(uglyJson);

console.log(beautyJson);
Enter fullscreen mode Exit fullscreen mode

Online runnable examples: https://dirask.com/posts/pqqJxp

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay