Create JSON Object in JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
var json = {};
var singleObj = [];
var obj = new Object; //Remember to declare an object so you can add key to it
obj.key1 = "value1";
obj.key2 = "value2";
singleObj.push(obj);
json["singleObj"] = singleObj;
var jsonString = JSON.stringify(json); //Change JSON Object to String
window.console.log(jsonString);
//{"singleObj":[{"key1":"value1","key2":"value2"}]}