Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

358
Views
How do I rewrite the JSON object in a file using file system in Node Js

I have came across the read and write operations using fs in Node Js.

My scenario is like, I have a file having the data like ,

[
{
    "Pref":"Freedom",
    "ID":"5545"
},
{
    "Pref":"Growth",
    "ID":"8946545"
}
]

I have to replace the Pref of the element whose ID is 5545 using Node js.

How can I do it. Thanks

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

To do what you want, you wound need to:

  1. read JSON data from the file with fs.readFile()
  2. parse the JSON with JSON.parse()
  3. find the correct object in the array
  4. change the object
  5. serialize the object back to JSON with JSON.stringify()
  6. write the file with fs.writeFile()

but this is not that simple as it may look like, because you will have to:

  • add locking to writes so that you never do two writes at the same time
  • add locking to reads so that you never read while the write is in progress
  • handle incorrect JSON
  • handle cases of objects that cannot be serialized
  • avoid blocking operations (those with "Sync" in their name) anywhere else then in the first tick of the event loop

Considering all of that you should consider using a database to store any data that changes. Some databases like Mongo, Postgres or Redis need to be run as standalone application either on the same or on a different server. Some embedded databases like SQLite don't need a standalone process and can be run directly in your application.

It's not that it is impossible to write to JSON files and then read those files as needed, but the amount of work that you'd have to do to synchronize the access to the data all without accidentally blocking the event loop in the process is much more difficult than just using any database as intended.

over 4 years ago · Santiago Trujillo Report

0

You have some data:

const data = [
  {
    "Pref":"Freedom",
    "ID":"5545"
  },
  {
    "Pref":"Growth",
    "ID":"8946545"
  }
]

First we need to find the element you want to change (use [0] to only select the first in case there are multiple items with ID 5545:

const objectToChange = data.filter(item => item.ID === "5545")[0]

And then change it!

objectToChange['Pref'] = "Liberty"

We can see the change reflected in the data object:

console.log(data)

//  [{
//    ID: "5545",
//    Pref: "Liberty"
//  },{
//    ID: "8946545",
//    Pref: "Growth"
//  }]
over 4 years ago · Santiago Trujillo Report

0

1- Load file: let json = JSON.parse(fs.readFileSync('file.json', 'utf-8'));

2- Update content:

json = json.map(el => {
  if(el.ID === "5545") {
    el.Pref = "TEST";
  }
  return el;
});

3- Save again maybe?

fs.writeFileSync('test.json', JSON.stringify(json), 'utf-8');

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!