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')
exit 1 indicates error, exit 0 indicates success
parse_weather('weather.json')
City: London Temperature: 15.5°C Condition: partly cloudy Humidity: 72%
parse_weather('weather.json')
City: Tokyo Temperature: 22.3°C Condition: clear sky Humidity: 65%
parse_weather('weather.json')
City: Moscow Temperature: -5.2°C Condition: snowy Humidity: 85%
Console output will appear here...
Are you sure?
You're making great progress