Convert Javascript object to JSON String example

Converting a JavaScript object to a JSON String is very easy with the help of JSON.stringify() method.

Let's take a look at an example:


JavaScript Object
const album = {
  name: "Hopes and Fears",
  artist: "Keane",
  year: 2004
};

Convert to JSON Object
const jsonAlbum = JSON.stringify(album);

Print the JSON Object
console.log(jsonAlbum);

Output:

{"name":"Hopes and Fears","artist":"Keane","year":2004}


Note: If the provided JavaScript Object is invalid you will get an SyntaxError while conversion to JSON String.

SyntaxError: Unexpected string literal "Keane". Expected a ':' following the property name 'artist'.

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!