0
0
Rubyprogramming~10 mins

JSON library basics in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load the JSON library.

Ruby
require '[1]'
Drag options to blanks, or click blank then click option'
Ajson
Bnet/http
Cdate
Dfileutils
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'net/http' instead of 'json' to load the JSON library.
Forgetting to require any library before using JSON methods.
2fill in blank
medium

Complete the code to parse a JSON string into a Ruby hash.

Ruby
data = JSON.[1]('{"name":"Alice","age":30}')
Drag options to blanks, or click blank then click option'
Adump
Bload
Cparse
Dgenerate
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSON.generate which converts Ruby objects to JSON string.
Using JSON.dump which also converts Ruby objects to JSON string.
3fill in blank
hard

Fix the error in the code to convert a Ruby hash to a JSON string.

Ruby
json_string = JSON.[1]({name: "Bob", age: 25})
Drag options to blanks, or click blank then click option'
Aparse
Bload
Cparse_json
Dgenerate
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSON.parse which is for reading JSON strings, not creating them.
Using JSON.load which is for loading JSON from files or strings.
4fill in blank
hard

Fill both blanks to create a JSON string from a Ruby hash and then parse it back.

Ruby
json_str = JSON.[1]({city: "Paris", country: "France"})
data = JSON.[2](json_str)
Drag options to blanks, or click blank then click option'
Agenerate
Bparse
Cload
Ddump
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSON.dump instead of generate for the first step.
Using JSON.load instead of parse for the second step.
5fill in blank
hard

Fill all three blanks to parse a JSON string, access a value, and generate a new JSON string.

Ruby
data = JSON.[1]('{"fruit":"apple","count":10}')
count = data[[2]]
new_json = JSON.[3]({fruit: "apple", count: count + 5})
Drag options to blanks, or click blank then click option'
Aparse
B"count"
Cgenerate
D"fruit"
Attempts:
3 left
💡 Hint
Common Mistakes
Using symbols like :count instead of string keys like "count".
Using JSON.load instead of parse.
Using JSON.dump instead of generate.