How to push json data from a json file into an array with a new set and update the file
You are inserting an array into the product array.
See the correct code below -
const products = [];
fs.readFile(pathToTheFile, (err, data) => { // get the data from the file
if(data != ''){
products = JSON.parse(data);
}
products.push(this);
fs.writeFile(pathToTheFile, JSON.stringify(products), (err) => {
console.log(err);
});
});
JavaScript push to array
It's not an array.
var json = {"cool":"34.33","alsocool":"45454"};
json.coolness = 34.33;
or
var json = {"cool":"34.33","alsocool":"45454"};
json['coolness'] = 34.33;
you could do it as an array, but it would be a different syntax (and this is almost certainly not what you want)
var json = [{"cool":"34.33"},{"alsocool":"45454"}];
json.push({"coolness":"34.33"});
Note that this variable name is highly misleading, as there is no JSON here. I would name it something else.
JSON array pushing
I tried and found the solution to it, since the att and values will be object after i push it
var temp = new Object();
temp["id"] = "po-0167";
temp["date"] = new Date(1980, 2, 24);
temp["quantity"] = 1;
temp["amount"] = 4;
temp["title"] = "A Book About Nothing";
bookorders.push(temp);
this will make it display in the datatable, the generic part will just be iterated through using the temp[att] = attributes[att];
How to push multiple json (response ) in single array?
Hi @Shreyash Kolhe you can try something like this.
const [data, setData] = useState([]);
with useState
you will have the data and a setter function to set data. In your Axios call, you can set the data as below.
setData(preData => [...preData, res.data]);
The preData
will hold the previous data which you current state data.
This will append the new data with the old data you have in your state. Please share the code next time it will help the community to help you out more effectively.
Cannot push JSON object into Array
var checkedIntervews = []var interviewModel = {};checkedIntervews.push(JSON.stringify(interviewModel));console.log(checkedIntervews);
(function arrayPush() {this.checkedIntervews = [];var interviewModel = {};this.checkedIntervews.push(JSON.stringify(interviewModel));console.log(this.checkedIntervews);})();
How can I push data to an array in a JSON file and save it?
When you import the contents of testlist.json
into the variable testlist
using require()
, you are loading the file's contents into memory. You will need to write back to the file after you have made changes to the testlist
variable if you want your modifications to persist. Otherwise, the changes that you make will be lost when the program's process exits.
You can use the writeFileSync()
method from the fs
module, as well as JSON.stringify()
, to write testlist
back into the testlist.json
file:
const fs = require("fs");
let testlist = require("../testlist.json");
// Your code where you modify testlist goes here
// Convert testlist to a JSON string
const testlistJson = JSON.stringify(testlist);
// Write testlist back to the file
fs.writeFileSync("../testlist.json", testlistJson, "utf8");
Edit: You should also use the readFileSync()
method (also from the fs
module), and JSON.parse()
to perform the initial reading of the JSON file, rather than require()
.
// This line
let testlist = require("../testlist.json");
// Gets replaced with this line
let testlist = JSON.parse(fs.readFileSync("../testlist.json", "utf8"));
You have to use JSON.parse
as well as fs.readFileSync
because when you are reading the file, it is read as a string, rather than a JSON object.
Related Topics
Check If Second String Is Rotation of Another String
What Does This Format Means T00:00:00.000Z
How to Call Make Angular Service Synchronous
Get List of Filenames in Folder With JavaScript
To Get a Popup Message After Submit
Filtering Multiple Items With Es6 Array Functions
How to Reload a Page With React-Router
Pause/Stop Videos into Tabs When I Clicked on a Tab
React Js Error: Is Not Defined React/Jsx-No-Undef
Vue.Js Add Objects to Existing Array
How to Restrict Past Dates in Html5 Input Type Date
How to Use Js to Open an HTML Select to Show Its Option List
Remove Whitespace Inside Json Object Key Value Pair
Jquery Ajax Post Results in 500 Internal Server Error
Prevent Swiping Between Web Pages in iPad Safari
How to Reload Datatables from Another Ajax Request With Onclick