1.0.5 • Published 6 years ago
read-write-json v1.0.5
JSON File Structure
// Json file
{
"users": [
{
"id": "0011",
"name": "New User1"
},
{
"id": "789",
"name": "nuser2@test.com"
}
],
"isSuccess": "New Value"
}
Code to Read from JSON file and Write to JSON File
const jsonData = require('read-write-json');
fit('Read Write from JSON Package', function() {
// --------Reading from JSON file----------------
let data = jsonData.readJSONFile('path to json file');
// To get all the data of json file
console.log(data);
// To get the specific key value
console.log(data.isSuccess);
// To get specific key value from an array
console.log(data.users[0].id);
// --------Writing to JSON file------------------
// Updating key value
data.isSuccess = 'New Value JSON';
jsonData.writeJSONFile('path to json file', data);
// Updating array key value
data.users[0].id = '0011';
jsonData.writeJSONFile('path to json file', data);
});