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);
Online runnable examples: https://dirask.com/posts/pqqJxp
Top comments (0)