1var obj = {
2 a: 1,
3 b: 2,
4 c: { d: 3, e: 4 } // object in object
5};
6
7// method 1 - simple output
8console.log(JSON.stringify(obj)); // {"a":1,"b":2,"c":{"d":3,"e":4}}
9
10// method 2 - more readable output
11console.log(JSON.stringify(obj, null, 2)); // 2 - indent spaces. Output below
12/*
13{
14 "a": 1,
15 "b": 2,
16 "c": {
17 "d": 3,
18 "e": 4
19 }
20}
21*/
1str = JSON.stringify(obj);
2str = JSON.stringify(obj, null, 4); // (Optional) beautiful indented output.
3console.log(str); // Logs output to dev tools console.
4alert(str); // Displays output using window.alert()