JSON & YAML

JSON and YAML are two text formats for serializing data, and Ruby's standard library converts between them and native hashes and arrays with a single method call in each direction.

Learn JSON & YAML in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll parse and generate JSON, symbolize keys, and round-trip a hash through YAML.

What You'll Learn in This Lesson

1️⃣ Parsing JSON into Ruby

require 'json' , then JSON.parse(string) returns a Hash/Array tree. By default object keys come back as strings ; pass symbolize_names: true to get symbol keys, which read more naturally as data[:name] .

2️⃣ Generating JSON from Ruby

JSON.generate(obj) and the shortcut obj.to_json produce a compact string. For human-readable output with indentation, use JSON.pretty_generate . Notice symbol keys come out as plain JSON strings.

3️⃣ YAML: Dump, Load & Round-Trip

YAML is the go-to for human-edited config. require 'yaml' , then YAML.dump(obj) writes a YAML string (note the leading --- document marker) and YAML.load(string) reads it back — a clean round-trip.

Your turn. Parse a small JSON object using symbol keys and read two values. Fill in each ___ .

Parse a JSON array of users, filter the admins, and emit a JSON summary. This parse → transform → generate flow is the heart of working with APIs. Run with ruby users.rb .

📋 Quick Reference — JSON & YAML

Practice quiz

What must you require before using JSON methods?

  • require 'parser'
  • import json
  • require 'json'
  • include JSON

Answer: require 'json'. require 'json' loads the standard-library JSON module.

What does JSON.parse return for a JSON object?

  • a Hash
  • a String
  • an Array
  • a Struct

Answer: a Hash. A JSON object parses into a Ruby Hash (arrays become Arrays).

By default, JSON.parse gives object keys as:

  • symbols
  • integers
  • nil
  • strings

Answer: strings. Keys come back as strings unless you request symbols.

Which option makes JSON.parse return symbol keys?

  • symbols: true
  • symbolize_names: true
  • as_symbols: true
  • keys: :symbol

Answer: symbolize_names: true. Pass symbolize_names: true to get symbol keys like data[:name].

Which produces a compact JSON string from a Ruby object?

  • obj.to_json
  • obj.to_s
  • obj.dump
  • obj.inspect

Answer: obj.to_json. to_json (or JSON.generate) produces a compact JSON string.

Which method produces indented, human-readable JSON?

  • JSON.generate
  • JSON.format
  • JSON.pretty_generate
  • JSON.indent

Answer: JSON.pretty_generate. JSON.pretty_generate adds indentation for readability.

What happens to Ruby symbols when converted to JSON?

  • they stay symbols
  • they become strings
  • they raise an error
  • they become nil

Answer: they become strings. JSON has no symbol type, so symbols become strings.

Which method serializes a Ruby object to YAML text?

  • YAML.parse
  • YAML.write
  • YAML.to_yaml
  • YAML.dump

Answer: YAML.dump. YAML.dump converts a Ruby object into a YAML string.

What marker begins a YAML document produced by YAML.dump?

  • ===
  • ---
  • %YAML
  • #yaml

Answer: ---. YAML documents begin with the --- document marker.

For untrusted YAML input you should prefer:

  • YAML.load with no limits
  • eval
  • YAML.safe_load
  • JSON.parse

Answer: YAML.safe_load. safe_load only allows basic types, avoiding object-injection risks.