Pretty print JSON in Node.js
When you read a JSON file, change the key or value, and write back to the same file, you will lose the JSON formatting.
For example:
{
"a": 1,
"b": 2,
"c": 3
}
You get
{"a":1,"a":2,"a":3}
Here is a way to pretty print JSON using JSON.stringify
. JSON.stringify
accepts a third parameter which defines white-space insertion. It can be a string or a number (number of spaces). Example:
JSON.stringify({ a:1, b:2, c:3 }, null, 4);
/* output:
{
"a": 1,
"b": 2,
"c": 3
}
*/
Latest Post
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync