Bird
Raised Fist0
Rubyprogramming~20 mins

JSON library basics in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JSON Mastery in Ruby
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using JSON?

Consider this Ruby code that uses the JSON library to parse a JSON string. What will be printed?

Ruby
require 'json'
json_str = '{"name":"Alice","age":30}'
obj = JSON.parse(json_str)
puts obj["name"]
ASyntaxError
B30
Cnil
DAlice
Attempts:
2 left
💡 Hint

Think about what JSON.parse returns and how to access values in the resulting object.

❓ Predict Output
intermediate
2:00remaining
What does this Ruby code output when generating JSON?

This Ruby code creates a hash and converts it to a JSON string. What is the output?

Ruby
require 'json'
hash = {"city" => "Paris", "country" => "France"}
puts JSON.generate(hash)
A{"city":"Paris","country":"France"}
B{city: "Paris", country: "France"}
C{"city":Paris,"country":France}
DSyntaxError
Attempts:
2 left
💡 Hint

Remember JSON keys and string values must be in double quotes.

🔧 Debug
advanced
2:00remaining
What error does this Ruby code raise when parsing JSON?

Look at this Ruby code snippet. What error will it raise when run?

Ruby
require 'json'
json_str = '{name: "Bob", age: 25}'
obj = JSON.parse(json_str)
ANoMethodError
BJSON::ParserError
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint

Check if the JSON string is valid JSON format.

🧠 Conceptual
advanced
2:00remaining
Which Ruby method converts a Ruby object to a JSON string?

Which method from the JSON library converts a Ruby object like a hash or array into a JSON formatted string?

AJSON.generate
BJSON.load
CJSON.parse
DJSON.read
Attempts:
2 left
💡 Hint

Think about the method that creates JSON text from Ruby objects.

🚀 Application
expert
2:00remaining
How many keys are in the Ruby hash after parsing this JSON string?

Given this Ruby code, how many keys does the resulting hash have?

Ruby
require 'json'
json_str = '{"a":1,"b":2,"c":{"d":4}}'
obj = JSON.parse(json_str)
puts obj.keys.size
A1
B4
C3
D2
Attempts:
2 left
💡 Hint

Count only the top-level keys in the parsed hash.

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

  1. 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.
  2. Step 2: Compare with other JSON methods

    JSON.generate does the opposite by converting Ruby objects into JSON strings.
  3. Final Answer:

    Converts a JSON string into a Ruby object -> Option C
  4. 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

  1. Step 1: Identify method to convert Ruby object to JSON string

    JSON.generate converts Ruby objects like hashes into JSON strings.
  2. 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.
  3. Final Answer:

    JSON.generate({"name" => "Alice"}) -> Option A
  4. Quick Check:

    generate = Ruby object to JSON string [OK]
Hint: Use generate to make JSON strings from Ruby hashes [OK]
Common Mistakes:
  • Using parse instead of generate
  • Confusing dump with parse
  • Passing Ruby hash directly to parse
3.

What is the output of this Ruby code?

require 'json'
json_str = '{"age":30,"city":"NY"}'
ruby_obj = JSON.parse(json_str)
puts ruby_obj["city"]
medium
A. SyntaxError
B. 30
C. nil
D. NY

Solution

  1. Step 1: Parse JSON string into Ruby hash

    The string {"age":30,"city":"NY"} is parsed into a Ruby hash with keys "age" and "city".
  2. Step 2: Access the value for key "city"

    Using ruby_obj["city"] returns the string "NY".
  3. Final Answer:

    NY -> Option D
  4. Quick Check:

    JSON.parse + key access = value [OK]
Hint: Parse JSON then access keys like a hash [OK]
Common Mistakes:
  • Expecting numeric output instead of string
  • Using symbol keys instead of string keys
  • Forgetting to require 'json'
4.

Identify the error in this Ruby code snippet:

require 'json'
json_str = '{"name": "Bob", "age": 25}'
ruby_obj = JSON.generate(json_str)
puts ruby_obj
medium
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

  1. Step 1: Check method usage

    JSON.generate expects a Ruby object (like a hash), but here it is given a JSON string.
  2. 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.
  3. Final Answer:

    Using JSON.generate on a JSON string instead of a Ruby object -> Option A
  4. 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

  1. Step 1: Convert Ruby array to JSON string

    JSON.dump([1, 2, 3]) converts the Ruby array into a JSON string.
  2. Step 2: Convert JSON string back to Ruby array

    JSON.load reads the JSON string and returns the Ruby array.
  3. 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).
  4. Final Answer:

    JSON.load(JSON.dump([1, 2, 3])) -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard