O'Reilly logo

JSON at Work by Tom Marrs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3. JSON in Ruby on Rails

We’ve shown how to use JSON in JavaScript, and in this chapter we’ll show how to use JSON with our second platform—Ruby on Rails (RoR).

We’ll cover the following:

  • Performing Ruby/JSON serialization/deserialization with MultiJson

  • Working with Ruby Objects and JSON

  • Understanding the importance of JSON camel casing

  • Using JSON with Minitest

  • Making RESTful API calls and testing the results with Minitest and jq

  • Building a simple JSON-based API with Rails 5

In our examples, we’ll make RESTful API calls to work with the data we deployed on json-server in Chapter 1. We’ll then create a more realistic JSON-based Web API. But before we develop a RESTful API, let’s start with the fundamentals of converting between Ruby and JSON.

Ruby on Rails Setup

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

Ruby JSON Gems

Several good JSON gems provide Ruby/JSON serialization/deserialization functionality, including these:

JSON

The default JSON gem provided in Ruby.

oj

Optimized JSON, considered by many to be the fastest Ruby-based JSON processor available.

yajl

Yet Another JSON Library.

There are many other JSON gems in addition to this list, and it’s hard to choose. Rather than forcing a developer to know how to use each JSON gem, MultiJson encapsulates this choice by providing a wrapper that invokes the most common ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required