tina
0
Q:

js print object as json

str = JSON.stringify(obj);
str = JSON.stringify(obj, null, 4); // (Optional) beautiful indented output.
console.log(str); // Logs output to dev tools console.
alert(str); // Displays output using window.alert()
2
const dataBase = {
  name:"Your_Name",
  Job: "Your_Job",
}

console.log(JSON.stringify(dataBase));
1
var obj = {
  a: 1,
  b: 2,
  c: { d: 3, e: 4 } // object in object
};

// method 1 - simple output
console.log(JSON.stringify(obj)); // {"a":1,"b":2,"c":{"d":3,"e":4}}

// method 2 - more readable output
console.log(JSON.stringify(obj, null, 2)); // 2 - indent spaces. Output below
/*
{
  "a": 1,
  "b": 2,
  "c": {
    "d": 3,
    "e": 4
  }
}
*/
0

New to Communities?

Join the community