How to update a value in a json file and save it through node js

0 votes

How do I update a value in a json file and save it through node.js? I have the file content:

var file_content = fs.readFileSync(filename);
var content = JSON.parse(file_content);
var val1 = content.val1;

Now I want to change the value of val1 and save it to the file.

Oct 12, 2020 in Node-js by kartik
• 37,510 points
25,272 views

1 answer to this question.

0 votes

Hello @kartik,

It's particularly useful if you're concerned for blocking the thread (likely).

const fs = require('fs');
const fileName = './file.json';
const file = require(fileName);

file.key = "new value";

fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
  if (err) return console.log(err);
  console.log(JSON.stringify(file));
  console.log('writing to ' + fileName);
});

The caveat is that json is written to the file on one line and not prettified. ex:

{
  "key": "value"
}

will be...

{"key": "value"}

To avoid this, simply add these two extra arguments to JSON.stringify

JSON.stringify(file, null, 2)

null - represents the replacer function. (in this case we don't want to alter the process)

2 - represents the spaces to indent.

Hope it helps!!

Get your Node.js Certification today to become a certified expert.

Thank you!!

answered Oct 12, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
0 answers

How to append to a file in Node?

I am trying to append a string to a ...READ MORE

Jul 9, 2020 in Node-js by kartik
• 37,510 points
756 views
0 votes
1 answer

How do you log content of a JSON object in Node.js?

Hello @kartik, Try this one: console.log("Session: %j", session); If the ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,880 points
681 views
0 votes
1 answer

How to host a Node.Js application in shared hosting?

Hello @kartik, You can run node.js server on a typical ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,880 points
6,673 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try this: var fs = require('fs'); var dir ...READ MORE

answered Jul 20, 2020 in Node-js by Niroj
• 82,880 points
530 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,689 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,898 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
818 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
756 views
0 votes
1 answer

How to create a directory if it doesn't exist using Node.js?

Hello @kartik, Try: var fs = require('fs'); var dir = ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
5,702 views
0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,880 points
590 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP