Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Hour 15. JSON APIs > Consuming JSON Data with Node.js

Consuming JSON Data with Node.js

Now that you have created a simple JSON-based web service, you can write a client to consume the data and do something with it. For this, you use a standard Node.js HTTP client and parse the data that you receive using JSON.parse.

The JSON.parse method parses a JSON string, reconstructing the original JavaScript object. You can see this at work by extending the earlier example to first convert a JavaScript object to JSON and then convert it back again:

var obj = {
  name : "Officer",
  surname : "Dibble"
}

console.log('JavaScript object:');
console.log(obj);

var json = JSON.stringify(obj);
console.log('JavaScript object to JSON:');
console.log(json);

var parsedJson = JSON.parse(json);
console.log('JSON to JavaScript object:');
console.log(parsedJson);


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial