0

I'm trying to figure out if it's possible to simply open up a JSON data script with nodejs and edit it... as javascript ...

// my json data
{
    people : [{
        name : 'Joe',
        hobby : 'hunting',
        job : 'accountant'
    },{
        name : 'William',
        hobby : 'chess',
        job : 'manager'
    }]
}

i just want to do something like e.g.

people[0].name = 'Joseph'

so i'm trying

fs.open('/path/to/file', 'r+', function(err, fd){
    // not really sure what to do from here...
})

there are plenty of answers about how to read/write text files... i just thought there might be an easier way for the case of a file in JSON

Whoops this is a repeat question - please see an earlier response here

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

Community
  • 1
  • 1
Petrov
  • 4,200
  • 6
  • 40
  • 61
  • possible duplicate of [How to update a value in a json file and save it through node.js](http://stackoverflow.com/questions/10685998/how-to-update-a-value-in-a-json-file-and-save-it-through-node-js) – Paul Sweatte Jan 06 '15 at 14:59

1 Answers1

3

Parse the JSON using JSON.parse(), modify the parsed object, turn it back into a new string using JSON.stringify(), then save the string to the file.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964