Appending To Json File In Javascript
I have a json file, employees.json, that I would like to append data to this object. The file looks like this: var txt = '{'employees':[' + '{'firstName':'Jerry','lastName':'Negrel
Solution 1:
vardata = JSON.parse(txt); //parse the JSONdata.employees.push({ //add the employee
firstName:"Mike",
lastName:"Rut",
time:"10:00 am",
email:"rut@bah.com",
phone:"800-888-8888",
image:"images/mike.jpg"
});
txt = JSON.stringify(data); //reserialize to JSON
Solution 2:
JSON stands for Javascript object notation so this could simply be a javascript object
var obj = {employees:[
{
firstname:"jerry"
... and so on ...
}
]};
When you want to add an object you can simply do:
object.employees.push({
firstname: "Mike",
lastName: "rut"
... and so on ....
});
Post a Comment for "Appending To Json File In Javascript"