Skip to content

Latest commit

 

History

History
37 lines (32 loc) · 989 Bytes

File metadata and controls

37 lines (32 loc) · 989 Bytes

Javascript Object Conversion.

Below is JavaScript Object


const book= {
    name:'Ego is enemy',
    author: 'Ryan holiday'
}

  • JSON.stringify() can be used to convert the javascript object to JSON.

const bookJson=JSON.stringify(book);
console.log(bookJson);// Result --> {"name":"Ego is enemy","author":"Ryan holiday"}

  • When we try to get the attribute of json object like below it will give undefined.

console.log("Second Result: "+ bookJson.name); //undefined
  
//but the javascript object attributes can be accessed like below.
console.log("Third Result: "+ book.name); //Ego is enemy

  • JSON.parse -To convert the JSON back to javascript object

const parseData=JSON.parse(bookJson)
console.log(parseData) //Result --> { name: 'Ego is enemy', author: 'Ryan holiday' }