📖 Scenario: You are working on a small Ruby program that needs to handle data in JSON format. JSON is a common way to store and share data, like a list of items with details.
🎯 Goal: You will create a Ruby program that uses the JSON library to convert a Ruby hash into a JSON string and then print it.
📋 What You'll Learn
Use the Ruby JSON library
Create a Ruby hash with specific key-value pairs
Convert the hash to a JSON string
Print the JSON string
💡 Why This Matters
🌍 Real World
JSON is used everywhere to send data between web browsers and servers, or to save data in files. Knowing how to convert Ruby data to JSON helps you work with APIs and web services.
💼 Career
Many programming jobs require working with JSON data. This skill is essential for backend development, web development, and data processing roles.
Progress0 / 4 steps
1
Create a Ruby hash with data
Create a Ruby hash called person with these exact key-value pairs: "name" => "Alice", "age" => 30, and "city" => "New York".
Ruby
Hint
Use curly braces {} to create a hash. Use strings as keys and values as shown.
2
Require the JSON library
Add a line to require the Ruby json library at the top of your code.
Ruby
Hint
Use require 'json' to load the JSON library.
3
Convert the hash to a JSON string
Create a variable called json_string and set it to the JSON string version of the person hash using JSON.generate(person).
Ruby
Hint
Use JSON.generate to convert a Ruby hash to a JSON string.
4
Print the JSON string
Write a line to print the json_string variable.
Ruby
Hint
Use puts to print the JSON string.
Practice
(1/5)
1.
What does the Ruby JSON.parse method do?
easy
A. Reads a file from disk
B. Converts a Ruby object into a JSON string
C. Converts a JSON string into a Ruby object
D. Writes a JSON string to a file
Solution
Step 1: Understand JSON.parse purpose
The method JSON.parse reads a JSON formatted string and converts it into a Ruby data structure like a hash or array.
Step 2: Compare with other JSON methods
JSON.generate does the opposite by converting Ruby objects into JSON strings.
Final Answer:
Converts a JSON string into a Ruby object -> Option C
Quick Check:
JSON.parse = JSON string to Ruby object [OK]
Hint: Remember: parse means read JSON into Ruby [OK]
Common Mistakes:
Confusing parse with generate
Thinking parse writes files
Assuming parse converts Ruby to JSON
2.
Which of the following is the correct way to convert a Ruby hash {"name" => "Alice"} to a JSON string?
easy
A. JSON.generate({"name" => "Alice"})
B. JSON.dump({"name" => "Alice"}, $stdout)
C. JSON.load({"name" => "Alice"})
D. JSON.parse({"name" => "Alice"})
Solution
Step 1: Identify method to convert Ruby object to JSON string
JSON.generate converts Ruby objects like hashes into JSON strings.
Step 2: Check other methods
JSON.parse reads JSON strings, JSON.load is similar to parse but less common, JSON.dump also converts Ruby objects but requires a string or IO target.
Final Answer:
JSON.generate({"name" => "Alice"}) -> Option A
Quick Check:
generate = Ruby object to JSON string [OK]
Hint: Use generate to make JSON strings from Ruby hashes [OK]
A. Using JSON.generate on a JSON string instead of a Ruby object
B. Missing require 'json' statement
C. Incorrect JSON string format
D. Using puts instead of print
Solution
Step 1: Check method usage
JSON.generate expects a Ruby object (like a hash), but here it is given a JSON string.
Step 2: Understand correct usage
To convert JSON string to Ruby object, use JSON.parse. To convert Ruby object to JSON string, use JSON.generate.
Final Answer:
Using JSON.generate on a JSON string instead of a Ruby object -> Option A
Quick Check:
generate needs Ruby object, not JSON string [OK]
Hint: Use parse for strings, generate for Ruby objects [OK]
Common Mistakes:
Confusing generate and parse
Passing string to generate
Ignoring error messages
5.
You have a Ruby array [1, 2, 3]. You want to convert it to a JSON string and then back to a Ruby array. Which sequence of methods correctly does this?
hard
A. JSON.dump(JSON.parse([1, 2, 3]))
B. JSON.load(JSON.dump([1, 2, 3]))
C. JSON.generate(JSON.parse([1, 2, 3]))
D. JSON.dump(JSON.load([1, 2, 3]))
Solution
Step 1: Convert Ruby array to JSON string
JSON.dump([1, 2, 3]) converts the Ruby array into a JSON string.
Step 2: Convert JSON string back to Ruby array
JSON.load reads the JSON string and returns the Ruby array.
Step 3: Verify other options
JSON.dump(JSON.parse([1, 2, 3])) tries to parse a Ruby array (invalid); JSON.generate(JSON.parse([1, 2, 3])) tries to parse a Ruby array (invalid); JSON.dump(JSON.load([1, 2, 3])) tries to load a Ruby array (invalid).
Final Answer:
JSON.load(JSON.dump([1, 2, 3])) -> Option B
Quick Check:
dump then load = Ruby array round-trip [OK]
Hint: Use dump then load for Ruby object to JSON and back [OK]
Common Mistakes:
Parsing Ruby objects instead of JSON strings
Using generate with parse incorrectly
Confusing dump/load with generate/parse
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.