A
A
Artem Melnykov2020-02-26 09:27:05
JSON
Artem Melnykov, 2020-02-26 09:27:05

How to write data to json?

Hello!
I have this JSON:

[
  {
    "username": "Vasiliy",
    "id": 1
  },
  {
    "username": "Ivan",
    "id": 2
  }
]

And how do I write the structure:
{
    "username": "User",
    "id": "UserID"
}

To a user.json file with Node.JS?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2020-02-26
@NickProgramm

const fs = require('fs');

let data = [
  ...
];

fs.writeFile('user.json', JSON.stringify(data, null, 2), (err) => {
  if (err) throw err;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question