Chapter 2. JSON in JavaScript

We’ve covered the basics of the JSON data interchange format, and in this chapter we’ll begin to develop applications with JSON. JSON began as a subset of the JavaScript definition for Objects and Arrays, but rest assured that JSON is now decoupled from JavaScript. JSON is language-agnostic and works across multiple platforms. Because JSON has its roots in JavaScript, this is where we begin our journey.

Here’s what we’ll cover:

  • Using JavaScript serialization/deserialization with JSON.stringify() and JSON.parse()

  • Working with JavaScript Objects and JSON

  • Making RESTful API calls and testing the results with Mocha/Chai Unit Tests

  • Building a small JSON-based web application

In our examples, we’ll leverage Node.js, scaffold a web application with Yeoman, and make RESTful API calls to pull in the data we created on json-server in the previous chapter. That’s a lot of moving pieces and parts, so we’ll iteratively build on each concept. But before we develop our web app, we need to start with the basics of JavaScript serialization/deserialization and Objects.

Node.js Setup

Before we go any further, let’s start building our development environment by installing Node.js. Please go to Appendix A, and follow the instructions in “Install Node.js”.

JSON Serialization/Deserialization with JSON.stringify() and JSON.parse()

Applications need to serialize (or flatten) their information into JSON in order to produce data for other applications in a platform-neutral ...

Get JSON at Work now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.