We often use `JSON.stringify`
to generate JSON for a given object. By default, it removes all spaces between items. It's not easy to scan the output, especially when using with `console.log`
.
Do you know that it's possible for us to generate a pretty output by passing the indentation level to the third parameter?
const person = {
firstName: 'John',
lastName: 'Doe',
ages: 42,
};
JSON.stringify(person, null, 2);
If you prefer tab for the indentations, then passing the `\t`
character:
JSON.stringify(person, null, '\t');
See also