Ruby Monstas

JSON with Ruby

JSON with Ruby

JSON is a popular format for
exchanging information between systems.
🖥️ ↔️ 📄 ↔️ 🖥️

That's mainly because it's easy to read and write for
humans 👩‍🦱 and machines 🤖.

Many programming languages have built-in support for
reading and writing JSON documents
... so does Ruby.

JSON Standard Library

Ruby comes with a JSON standard library that helps us working with JSON documents.
require 'json'

Reading and Writing

Basically we're only going to need 2 functionalities from that library:
  • JSON.parse
    converts a Ruby-String containing JSON into a Ruby object.
  • JSON.generate
    converts a Ruby object into a Ruby-String containing JSON.

Parse JSON into a Ruby object

we can read a JSON object and convert it into a Hash or Array.
require 'json'

movie_as_json = '{
  "title": "Shrek",
  "rating": 90,
  "filmingLocation": null
movie = JSON.parse(movie_as_json)

puts movie
# => {"title"=>"Shrek", "rating"=>90, "filmingLocation"=>nil}

Note that the key "filmingLocation" has not been changed to snake_case, but that's OK.

Generate JSON from a Hash

we can generate a JSON object from any Hash or Array.
require 'json'

actress = {
  name: "Emma Watson",
  academy_awards: nil
actress_as_json = JSON.generate(actress)

puts actress_as_json
# => {"name": "Emma Watson", "academy_awards": null}

Note that the key "academy_awards" has not been changed to camelCase either.

What's the point?

Why would we do all of this "generate-parse"-cycle?

Data transmission

Within our own application it does not make much sense to use JSON.

But if our application wants to "talk" to another application (that may not be built in Ruby), we often use JSON for communication.

Communication between applications

This is how it could work if we need to interact with another application:
  1. Our application generates a JSON document with our "message" and sends it to the other application.
  2. The other application parses our "message" with their programming language. They process it, generate a response, as JSON, and send that back to us.
  3. Finally we receive and parse their response and continue with our logic...

That's an API !

That other application we call an


Do you know any APIs?

Or do you know any services that you think they could be APIs?

There are a gazillion APIs

  • 👥 Social (Twitter, Facebook, Instagram, ...)
  • 💶 Finance (Stock market, Trading, Currency, ...)
  • 🌦 Weather (Live weather, Forecasts, ...)
  • 🌍 Geography (Maps, Directions, ...)
  • 📽 Lifestyle (Books, Movies, Music, ...)
  • 🚌 Transportation (Flight Radar, Train Schedules, ...)
  • Many, many more

Practical example

Let's build a (fictional) weather app. 🌦
For a given location we want to know the temperature and conditions for the next 24 hours.
Our application won't do the forecasting itself. Therefore we use a (fictional) API on a weather-server, which provides the data we need.

What's the weather in... Edinburgh.

Let's see how this could work

request = { location: "Edinburgh" }

request_as_json = JSON.generate(request)
# => "{\"location\":\"Edinburgh\"}"

answer_from_server = WeatherServer.send(request)
# => "{\"tempC\": 14, \"conditions\": \"Heavy Rain\"}"

forecast = JSON.parse(answer_from_server)
# => {"tempC"=>14, "conditions"=>"Heavy Rain"}
Note that we entirely hide how the class
is implemented, since it's not in our focus here.
How to communicate over a network is covered in another lesson.

What questions do you have?