How to Pretty Print JSON String in JavaScript? Last Updated : 21 Dec, 2024 Comments Improve Suggest changes Like Article Like Report To pretty print JSON, you can format JSON strings with proper indentation, making them easy to read and debug. In JavaScript, you can achieve this using JSON.stringify() with optional parameters to specify the indentation level.Pretty printing JSON adds line breaks and spaces for readability.It is commonly used for debugging or displaying JSON in a readable format.The JSON.stringify() method helps format JSON data with indentation. JavaScript const obj = { name: "Rahul", age: 30, city: "Mumbai" }; const pretty = JSON.stringify(obj, null, 2); console.log(pretty); Output{ "name": "Rahul", "age": 30, "city": "Mumbai" } How Pretty Printing Works1. JSON.stringify() with IndentationThe JSON.stringify() method takes three arguments:The JSON object to stringify.A replacer function (optional).The number of spaces for indentation (optional).2. Proper Spacing for ReadabilityBy providing an indentation value, you can control the amount of spacing for nested elements.SyntaxJSON.stringify(value[, replacer[, space]]);value: The JSON object or array to convert into a string.replacer (optional): A function or array to filter properties.space (optional): Number of spaces or a string for indentation.Format JSON with 4 Spaces JavaScript const data = { name: "Anjali", profession: "Developer", skills: ["JavaScript", "Python"] }; const formatted = JSON.stringify(data, null, 4); console.log(formatted); Output{ "name": "Anjali", "profession": "Developer", "skills": [ "JavaScript", "Python" ] } Use Cases of Pretty Printing JSON1. Debugging JSON DataWhen dealing with complex JSON data, pretty printing helps debug issues effectively. JavaScript const raw = '{"name":"Vikas","details":{"age":25,"city":"Delhi"}}'; const obj = JSON.parse(raw); console.log(JSON.stringify(obj, null, 2)); Output{ "name": "Vikas", "details": { "age": 25, "city": "Delhi" } } 2. Saving Pretty JSON to FilesPretty JSON can be written to files for better organization. JavaScript const fs = require('fs'); const obj = { name: "Pooja", age: 28, city: "Bangalore" }; fs.writeFileSync('data.json', JSON.stringify(obj, null, 2)); console.log('JSON saved in a pretty format.'); 3. Display JSON in Web ApplicationsYou can display formatted JSON in your web app using <pre> tags. JavaScript const jsonS = '{"name":"Amit","hobbies":["Cricket","Reading"]}'; const formatted = JSON.stringify(JSON.parse(jsonS), null, 2); document.body.innerHTML = `<pre>${formatted}</pre>`; Comment More infoAdvertise with us Next Article How to Pretty Print JSON String in JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JSON JavaScript-Questions JavaScript-JSON +1 More Similar Reads How to Print a String in JavaScript ? In JavaScript, printing a string means displaying the contents of a string variable or literal to the console, webpage, or any other output medium. Strings are a fundamental data type in JavaScript, representing sequences of characters enclosed within single ('') or double ("") quotes. Table of Cont 2 min read How to Create JSON String in JavaScript? JSON strings are widely used for data interchange between a server and a client, or between different parts of a software system. So converting objects to JSON strings is very important for good client-server communication. Below are the following approaches to creating a JSON string: Table of Conte 2 min read How to Parse JSON in JavaScript ? Parse JSON in JavaScript, accepting a JSON string as input and returning a corresponding JavaScript object with two methods, using JSON.parse() for parsing JSON strings directly and employing the fetch API to parse JSON responses from web APIs. These techniques are crucial for seamless data manipula 2 min read How to Serialize JSON in JavaScript ? JSON (JavaScript Object Notation) serialization is a fundamental concept in JavaScript, allowing the conversion of JavaScript objects into strings that can be easily transmitted over a network or stored in a file. We will explore how to serialize JSON in JavaScript using JSON.stringify(). Approach I 1 min read How to Master JSON in JavaScript? JSON is a text format for representing structured data, typically in the form of key-value pairs. It primarily sends data between a server and a client, especially in web APIs.Objects are enclosed in curly braces {} and contain key-value pairs.Arrays are enclosed in square brackets [] and hold value 5 min read How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used 3 min read How to parse JSON object using JSON.stringify() in JavaScript ? In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: 2 min read How to Convert String to JSON in JavaScript? In JavaScript, converting a string to JSON is important for handling data interchangeably between server and client, parsing external API responses, and storing structured data in applications. Below are the approaches to converting string to JSON in JavaScript: Table of Content Using JSON.parse()Us 2 min read How to Convert a Map to JSON String in JavaScript ? A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A 2 min read How to JSON Stringify an Array of Objects in JavaScript ? In JavaScript, the array of objects can be JSON stringified for easy data interchange and storage, enabling handling and transmission of structured data. The below approaches can be utilized to JSON stringify an array of objects.Table of ContentUsing JSON.stringify with a Replacer Function Using a C 3 min read Like