Converting an object to JSON and back
JavaScript Object Notation (JSON) is very closely related to JavaScript objects because it's a subset of JavaScript. This task will demonstrate how to use the building blocks of JSON conversion: JSON.parse
and JSON.stringify
.
Getting ready
We'll need to create two new files called profiles.js
, which will hold the data we'll be converting into JSON and back.js
, which will be our main file for this recipe.
How to do it…
Let's create the object that we'll later be converting to JSON, in profiles.js
as follows:
module.exports = { ryan: { name: "Ryan Dahl", irc: "ryah", twitter: "ryah", github: "ry", location: "San Francisco, USA", description: "Creator of node.js" }, isaac: { name: "Isaac Schlueter", irc: "isaacs", twitter: "izs", github: "isaacs", location: "San Francisco, USA", description: "Former project gatekeeper, CTO npm, Inc." }, timothy: { name: "Timothy J Fontaine", irc: "tjfontaine"...