Working with API data is a fundamental skill in modern development. This drill teaches you to parse JSON responses, extract nested data, handle missing fields gracefully, and format output for users. You'll learn JSON parsing, safe nested hash navigation, data validation, and error handling—essential skills for building applications that work with external data sources.
JSON.parse(File.read(file)) reads and parses JSON in one step
Use hash.dig('key1', 'key2', 0) for safe nested access
rescue JSON::ParserError catches invalid JSON
Check if value.nil? to detect missing fields
Arrays in JSON become Ruby arrays - access with index [0]
Nested hashes: data['main']['temp'] or data.dig('main', 'temp')
parse_weather('weather.json')
City: London Temperature: 15.5°C Condition: partly cloudy Humidity: 72%
result = parse_weather('weather.json')
puts result
City: London Temperature: 15.5°C Condition: partly cloudy Humidity: 72% true
parse_weather('weather.json')
puts 'Temp OK'
City: London Temperature: 15.5°C Condition: partly cloudy Humidity: 72% Temp OK
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept