nodejs json beautify

To beautify JSON in Node.js, you can use the json-beautify package. Follow the steps below:

  1. First, you need to install the json-beautify package. Open your terminal and run the following command:
npm install json-beautify
  1. Once the package is installed, you can require it in your Node.js script:
const beautify = require('json-beautify');
  1. Next, you need to define your JSON object. Let's say you have a JSON object called myJson:
const myJson = {
  name: 'John',
  age: 30,
  city: 'New York'
};
  1. Now, you can use the beautify function to beautify your JSON object:
const beautifiedJson = beautify(myJson, null, 2, 80);

The beautify function takes four arguments: - The JSON object you want to beautify (myJson in this case). - The replacer function (null in this case). You can use a replacer function to modify the values or omit certain properties from the output. - The number of spaces to use for indentation (2 in this case). - The number of characters per line (80 in this case).

  1. Finally, you can log the beautified JSON to the console:
console.log(beautifiedJson);

That's it! The json-beautify package will format your JSON object with proper indentation and line breaks, making it more readable.