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
- Reading and Writing JSON
- Working with CSV
- YAML Configuration
- Choosing a Format