Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Javascript - JSON

Back


JSON


JSON.parse(): JSON string to JS object

let json_str =
  '{ "employees" : [' +
  '{ "firstName":"John" , "lastName":"Doe" },' +
  '{ "firstName":"Anna" , "lastName":"Smith" },' +
  '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

const json_obj = JSON.parse(json_str);

console.log(json_obj);
// {
//   employees: [
//     { firstName: 'John', lastName: 'Doe' },
//     { firstName: 'Anna', lastName: 'Smith' },
//     { firstName: 'Peter', lastName: 'Jones' }
//   ]
// }
console.log(typeof json_obj); //object

JSON.stringify(): JS object into string

const obj = { name: "John", age: 30, city: "New York" };
const json_str = JSON.stringify(obj);

console.log(json_str); //{"name":"John","age":30,"city":"New York"}
console.log(typeof json_str); //string

TOP