I am a new to JSON and facing problems creating a very simple array.
I have the key,value pair received from an HTML form:
frm1 = {"fname":"John","lname":"Doe","location":"CA"};
frm2 = {"fname":"Jenny","lname":"Doe","location":"CA"};
I want to create a JSON like below:
{
"employee":[
{"fname":"John","lname":"Doe","location":"CA"},
{"fname":"Jenny","lname":"Doe","location":"CA"}
]}
Trying to push the first form data(frm1) only by below code is not working.
var form1 = {"employee":[]};
form1.employee = frm1;
console.log(JSON.stringify(form1)); // prints form1 :{"employee" : ["fname","lname","location"]}
Only keys are printed. Please suggest.
console.log(typeof frm1)will say eitherobjectorstring. E.g.console.log(typeof '{"fname": "john"}')will say it's a string which needs to be parsed whereasconsole.log(typeof {"fname": "john"})will say that it's a good ol' object.