JSON and YAML

JSON stdlib

The json library converts between Ruby objects and JSON text. Require it once at the top of the file.

Hashes become JSON objects, arrays stay arrays, and symbols turn into quoted names. Dates travel as strings inside such payloads, formatted as in Time and date.

require "json"

Parse JSON

parse reads a JSON string into hashes and arrays. Keys arrive as strings, so fetch them with string keys.

Symbol access needs an extra option, which beginners can skip until the shape of data feels familiar.

require "json" data = JSON.parse("{\"name\": \"Ada\"}") puts data["name"]

ruby parse_json.rb

Ada

Generate JSON

generate turns hashes and arrays back into text. Build the structure from plain Ruby objects and let the library handle quoting.

Pretty output with extra spaces exists for config files humans read, while compact output suits network traffic.

require "json" user = { name: "Ada", age: 36 } puts JSON.generate(user)

ruby generate_json.rb

{"name":"Ada","age":36}

YAML with psych

Psych is the YAML engine behind the yaml library. dump writes objects as readable YAML, and safe_load reads trusted data back.

Prefer safe_load over load for outside text: it blocks surprise object creation from hostile files.

require "yaml" data = YAML.safe_load("name: Ada") puts data["name"]

ruby parse_yaml.rb

Ada

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? require "json" data = JSON.parse("{\"name\": \"Ada\"}") puts data["name"]

    Answer

    Ada

    parse builds a hash with string keys, and puts prints the value without quotes.

  2. What does this program print? require "json" user = { name: "Ada", age: 36 } puts JSON.generate(user)

    Answer

    {"name":"Ada","age":36}

    generate writes compact JSON, and the hash keys become quoted names.

  3. What does this program print? require "yaml" data = YAML.safe_load("name: Ada") puts data["name"]

    Answer

    Ada

    safe_load builds a hash from the YAML, and puts prints the string without quotes.

  4. What does this program print? require "json" p JSON.parse("[1, 2]")

    Answer

    [1, 2]

    A JSON array becomes a Ruby array, and p prints it in inspect form.

  5. What does this program print? require "json" p JSON.parse("{\"a\": 1}")["a"]

    Answer

    1

    JSON keys arrive as strings, so the string key finds the value.

  6. Which direction does JSON.generate take?

    Answer

    Ruby objects into JSON text

    parse is the reading direction; generate is the writing one.

  7. Why prefer YAML.safe_load over YAML.load for outside text?

    Answer

    It blocks surprise object creation from hostile files

    safe_load permits only the basic types, which is what untrusted input needs.

  8. What does this program print? require "json" p JSON.parse("{\"a\": 1}") == { "a" => 1 }

    Answer

    true

    parse produces an ordinary hash, and hashes compare by their contents.

  9. When is JSON.pretty_generate the right call?

    Answer

    For a file a person reads, where extra spaces help

    Compact output suits the wire; pretty output suits a config a human edits.

  10. What does this program print? require "json" p JSON.generate([1, "two"])

    Answer

    "[1,\"two\"]"

    generate writes compact JSON without a space after the comma, and p quotes the whole string.

Article author: Arthur Isaev