Adding json file to javascript

I have a json file, employee.json, which I would like to add to this object. The file is as follows:

var txt = '{"employees":[' + '{"firstName":"Jerry","lastName":"Negrell","time":"9:15 am","email":" jerry@bah.com ","phone":"800-597-9405","image":"images/jerry.jpg" },' + '{"firstName":"Ed","lastName":"Snide","time":"9:00 am","email":" edward@bah.com ","phone":"800-597-9406","image":"images/ed.jpg" },' + '{"firstName":"Pattabhi","lastName":"Nunn","time":"10:15 am","email":" pattabhi@bah.com ","phone":"800-597-9407","image":"images/pattabhi.jpg" }'+ ']}'; 

I'd like to add:

  • Name: Mike
  • LastName: Track
  • time: 10 a.m.
  • Email: rut@bah.com
  • phone: 800-888-8888
  • images: images /mike.jpg

to employee.json.

How to do it?

+5
source share
2 answers
 var data = JSON.parse(txt); //parse the JSON data.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 
+9
source

JSON stands for Javascript object designation, so it could just be a javascript object

 var obj = {employees:[ { firstname:"jerry" ... and so on ... } ]}; 

If you want to add an object, you can simply do:

 object.employees.push({ firstname: "Mike", lastName: "rut" ... and so on .... }); 
+3
source

Source: https://habr.com/ru/post/900079/


All Articles