Tsallaka zuwa abun ciki
Justice Tech School

JSON and YAML

The json library converts Ruby hashes and arrays to text with to_json and JSON.parse back. YAML is common for configuration.

Example
require "json"
require "yaml"
h = { name: "Ada", tags: ["rb", "js"] }
s = h.to_json
puts s
puts JSON.parse(s, symbolize_names: true)[:tags].inspect
puts JSON.pretty_generate(h)
puts({ "a" => 1 }.to_yaml)

Result

{"name":"Ada","tags":["rb","js"]}
["rb", "js"]
{
  "name": "Ada",
  "tags": [
    "rb",
    "js"
  ]
}
---
a: 1

Exercise

Read JSON text.

JSON.(text)

Quiz Test

Which converts a hash to JSON?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.