In this post, you learn examples in Nodejs
- How to pretty print and format json object
- How to read the JSOn object
- Write json file
How to pretty print jSON object in Nodejs Application
PrettyPrint JSOn is easier to read instead of a normal json string and It contains tabs and spaces.
`Pretty print ’ is a way of printing JSON objects with correct indentation, tabs, and spaces and these can be easily read by developers.
This will be very useful for the inspection of a JSON object during debugging.
There are multiple ways we can do it,
Using the JSON.stringify method
JSON stringify method Convert the Javascript object to json string by adding the spaces to the JSOn string and printing in an easily readable format.
JSOn string contains objects without spaces in employee.json
[{"name":"eric","id":"1"},{"name":"andrew","id":"2"},{"name":"john","id":"3"},{"name":"Flintoff","id":"4"},{"name":"Greg","id":"5"},{"name":"Francis","id":"6"}]
Here is nodejs pretty print example
var fs = require('fs');
fs.readFile('employee.json', 'utf8', function (err, data) {
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(JSON.parse(data), null, 4));
}
});
First, Create the native filesystem object fs
- read the file using readFile, which returns a callback of error and data.
- returned pretty print json object before parsing json content using JSON object
using prettier npm library
prettier is a code formatter that has support for major programming languages.
First, Install a prettier npm library
npm install prettier --save-dev
Here is a code for Prettier to format json
var jsonData = '{ "active": true, "id": 1, "name": "john" }';
console.log(prettier.format(JSON.stringify(jsonData), { semi: false, parser: "json" }));
How to read external JSON files in Nodejs Application
It is easy to read json data files in Javascript.
There are multiple ways we can achieve it.
using the readFile method to asynchronous read json file
Let’s have an employee.json
file that contains the following data.
[
{
"name": "eric",
"id": "1"
},
{
"name": "andrew",
"id": "2"
},
{
"name": "john",
"id": "3"
},
{
"name": "Flintoff",
"id": "4"
},
{
"name": "Greg",
"id": "5"
},
{
"name": "Francis",
"id": "6"
}
]
JSOn files can be parsed in Asynchronous and Synchronous ways.
How to Asynchronous read and parse JSON file
here is a readFile
example to read the local external JSON file
readFile is an asynchronous operation with callback holding err, an error of file read status, data holds the content of the file
var fs = require('fs');
fs.readFile('employee.json', 'utf8', function (err, data) {
if (err) {
console.log(err);
} else {
console.log(JSON.parse(data));
}
});
Another way, parse the json file synchronously using the required in nodejs.
Here is a user.json file
{ "active": true, "id": 1, "name": "john" }
In Node.js, Import json file using the require keyword and It holds the content of the file as a javascript object.
You can access object properties using the object.properties syntax to get the value
var user = require('user.json');
console.log(user.active + ' ' + user.name);
How to Write data to JSOn file in Nodejs Application?
Here is a code for Nodejs write JSON file example
fs module
is being used to read and write files from/to a disk.
fs.writeFile
writes a javascript object to json file in an asynchronous way.
var fs = require("fs");
var jsonObj = {
id: 1,
name: "Franc"
};
fs.writeFile("./output.json", JSON.stringify(jsonObj, null, 2), (err) => {
if (err) { console.error(err); return; };
console.log("Created a JSOn file");
});