0Pricing
Ruby Academy · Lesson

Reading and Writing JSON

JSON.parse and generate.

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for data exchange. It maps cleanly to Ruby hashes and arrays.

Require the json library to use it.

require 'json'
data = { name: 'Ruby', year: 1995 }
puts(JSON.generate(data))

Parsing JSON Text

JSON.parse turns a JSON string into Ruby objects: objects become Hashes, arrays become Arrays.

require 'json'
text = '{"name":"Ruby","year":1995}'
h = JSON.parse(text)
puts(h['name'])
puts(h['year'])

All lessons in this course

  1. Reading and Writing JSON
  2. Working with CSV
  3. YAML Configuration
  4. Choosing a Format
← Back to Ruby Academy