json
  1. json-beautifier

Beautifier - (JSON Tools)

Syntax

To beautify JSON data, use the following syntax:

JSON.stringify(yourJSONdata, null, 2)

Where:

  • yourJSONdata is the JSON data that you want to beautify.
  • null is the replacer parameter. You can pass a function or an array of strings/numbers to modify the JSON data before it is stringified.
  • 2 is the space parameter, which specifies the number of spaces to use for indentation.

Example

Here's an example of how to beautify JSON data:

{
  "name": "John Doe",
  "age": 30,
  "email": "johndoe@example.com"
}
const jsonData = {"name":"John Doe","age":30,"email":"johndoe@example.com"};

const beautifiedJsonData = JSON.stringify(jsonData, null, 2);

console.log(beautifiedJsonData);

Output

The above example will produce the following output:

{
  "name": "John Doe",
  "age": 30,
  "email": "johndoe@example.com"
}

Explanation

JSON.stringify() function is used to convert the given JSON data into a string format. In the example, the third parameter of JSON.stringify() is used to pass the number of spaces required for the indentation.

Use

Beautifying JSON data is helpful when working with large JSON data files that need proper formatting to be easily readable. You can use the beautified JSON data for testing, debugging, or displaying it on the user interface.

Important Points

  • The beautify tool does not modify the content of the data but only reformats it for a better display.
  • JSON.stringify() can accept a function as a parameter that can modify or remove values before the JSON data is stringified.
  • JSON data should have a valid syntax; otherwise, the beautify tool will not work.

Summary

The Beautifier tool helps you format JSON data for easy readability. The JSON.stringify() function converts the JSON data into a string and accepts two optional parameters to modify the returned string. You can use the beautify tool to debug, test or display the JSON data. Take care to ensure that the JSON data has a valid syntax, or else the beautify tool may not work.

Published on: