D
D
danyadev2018-02-24 17:14:30
JavaScript
danyadev, 2018-02-24 17:14:30

How to add data to JSON file so it is readable with NodeJS?

I have a JSON file with this entry (for example):

{
  "user1": {
    "login": "abcde",
    "password": 12345
  }
}

How can I add another user there and change, for example, the login of the first one?
UPD. Decision:
var file = JSON.parse(fs.readFileSync('./renderer/file.json', 'utf-8'))
file.user1.password = 23456
fs.writeFileSync('./renderer/file.json', JSON.stringify(file, null, 2));

This code writes these lines to a JSON file (with formatting):
{
  "user1": {
    "login": "abcde",
    "password": 23456
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2018-02-24
@danyadev

Read file, change data, write to file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question