Answer the question
In order to leave comments, you need to log in
How to add object to json file?
In general, I want to send
[
{
"id": 1
}
]
Answer the question
In order to leave comments, you need to log in
You need to overwrite the file
https://nodejs.org/api/fs.html
https://metanit.com/web/nodejs/2.8.php
const Json = require('./my.json');
const fs = require("fs");
Json.push({
userId: 2,
id: 2
});
fs.writeFileSync("my.json", JSON.stringify(Json))
If this is a file, then you must first read it, then convert the string to JSON, and only then work with it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question