How can I format JSON stringify pretty to be easily readable for humans, focusing on indentation, whitespace, and possibly adding visual enhancements like colors and font styles?
You can use JSON.stringify() with additional parameters to format JSON for better readability:
const data = { "name": "John", "age": 30, "city": "New York" };
const formattedJson = JSON.stringify(data, null, 2);
console.log(formattedJson);
In this example:
JSON.stringify(data, null, 2) formats data with 2 spaces of indentation. This method ensures that JSON is structured and easy to read by human eyes.
Most modern web browsers provide a built-in feature to display JSON in a formatted and interactive way:
const data = { "name": "John", "age": 30, "city": "New York" };
console.log(data); // Displays the JSON object in a formatted way in the browser console
By logging the JSON object directly to the console, browsers often present it in an expandable and readable format, aiding in visual clarity.
For more enhanced visual formatting, you can use JSON viewer browser extensions:
- Chrome: Install extensions like “JSON Formatter” or “JSON Viewer.”
- Firefox: Use extensions such as “JSON Lite” or “JSONView.”
- These extensions typically format JSON with indentation, color coding, and collapsible sections, enhancing readability and making it easier to navigate through large JSON datasets.