How to beautifully output JSON in a readable format with indentation in JavaScript?
Hello everyone! I just started learning JavaScript and encountered this problem. I receive data from an API in JSON format, but when I try to output it to the console or on a page, everything is displayed as one long string without spaces or line breaks. This is very inconvenient for debugging and data analysis.
For example, I have this object:
```javascript
const user = {
name: "Ivan",
age: 30,
address: {
city: "Moscow",
street: "Lenina",
house: 10
},
hobbies: ["programming", "reading", "sports"]
};
```
When I do `console.log(user)`, the browser shows it as a tree, which is fine. But if I try to send this object through `JSON.stringify()` and display it on the page as text, I get:
```text
{"name":"Ivan","age":30,"address":{"city":"Moscow","s
No answers yet.
Log in to leave an answer
Blogs