0
0
Rubyprogramming~10 mins

JSON library basics in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - JSON library basics
Start
Require 'json'
Create Ruby object
Convert to JSON string
Parse JSON string back
Use Ruby object
End
This flow shows how to load the JSON library, convert Ruby objects to JSON strings, and parse JSON strings back to Ruby objects.
Execution Sample
Ruby
require 'json'

person = {"name" => "Alice", "age" => 30}

json_str = JSON.generate(person)

ruby_obj = JSON.parse(json_str)

puts ruby_obj["name"]
This code converts a Ruby hash to a JSON string and then parses it back to a Ruby hash, printing the name.
Execution Table
StepActionCode/ExpressionResult/ValueNotes
1Require JSON libraryrequire 'json'JSON module loadedMakes JSON methods available
2Create Ruby hashperson = {"name" => "Alice", "age" => 30}{"name"=>"Alice", "age"=>30}Ruby hash with keys and values
3Convert Ruby hash to JSON stringjson_str = JSON.generate(person){"name":"Alice","age":30}JSON string format
4Parse JSON string back to Ruby hashruby_obj = JSON.parse(json_str){"name"=>"Alice", "age"=>30}Ruby hash recreated
5Access value from Ruby hashputs ruby_obj["name"]AlicePrints the name value
6End--Program ends
💡 All steps completed successfully, program ends after printing 'Alice'
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
personnil{"name"=>"Alice", "age"=>30}{"name"=>"Alice", "age"=>30}{"name"=>"Alice", "age"=>30}{"name"=>"Alice", "age"=>30}
json_strnilnil{"name":"Alice","age":30}{"name":"Alice","age":30}{"name":"Alice","age":30}
ruby_objnilnilnil{"name"=>"Alice", "age"=>30}{"name"=>"Alice", "age"=>30}
Key Moments - 3 Insights
Why do we need to require 'json' before using JSON methods?
The require 'json' line loads the JSON library so Ruby knows how to convert objects to and from JSON. Without it, JSON.generate and JSON.parse won't work (see Step 1 in execution_table).
Why does the Ruby hash look different from the JSON string?
Ruby hashes use => for key-value pairs and keys can be symbols or strings. JSON strings use colon : and keys are always strings. This difference is shown in Step 2 vs Step 3 in execution_table.
What type is ruby_obj after parsing JSON?
After JSON.parse, ruby_obj is a Ruby hash, not a string. It has the same structure as the original hash (Step 4 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of json_str after Step 3?
A{"name":"Alice","age":30}
B{"name"=>"Alice", "age"=>30}
Cnil
DJSON module loaded
💡 Hint
Check the 'Result/Value' column for Step 3 in execution_table.
At which step does ruby_obj get assigned a Ruby hash?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Result/Value' columns in execution_table for when ruby_obj is assigned.
If we skip requiring 'json', what happens when calling JSON.generate?
AIt works normally
BIt raises an error because JSON is not loaded
CIt returns nil
DIt prints the Ruby hash directly
💡 Hint
Refer to Step 1 in execution_table and key_moments about requiring the library.
Concept Snapshot
require 'json' to load JSON methods
Use JSON.generate(obj) to convert Ruby object to JSON string
Use JSON.parse(str) to convert JSON string back to Ruby object
JSON keys are always strings; Ruby hash keys can be symbols or strings
Parsed JSON returns Ruby hashes and arrays
Always require 'json' before using JSON methods
Full Transcript
This example shows how to use Ruby's JSON library. First, require 'json' loads the library so you can use JSON methods. Then, a Ruby hash named person is created with keys 'name' and 'age'. Using JSON.generate, this hash is converted into a JSON string format. Next, JSON.parse converts the JSON string back into a Ruby hash named ruby_obj. Finally, accessing ruby_obj['name'] prints 'Alice'. The execution table traces each step, showing variable values and actions. Key moments clarify why requiring the library is necessary, the difference between Ruby hashes and JSON strings, and the type of object returned by parsing JSON. The quiz tests understanding of these steps. This process is essential for working with JSON data in Ruby.