Challenge

Problem

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.

Difficulty: Intermediate

Instructions

  1. Read weather data from weather.json file
  2. Parse JSON response
  3. Extract and display:
    • City name
    • Current temperature (rounded to 1 decimal)
    • Weather condition description
    • Humidity percentage
  4. Format output:
    'City: [name]'
    'Temperature: [temp]°C'
    'Condition: [description]'
    'Humidity: [value]%'
  5. Handle errors:
    • Invalid JSON: 'Error: Invalid JSON format'
    • Missing data fields: 'Error: Incomplete weather data'
  6. Exit with status 1 on error, 0 on success

Files

Editable
Read-only

Hints

Hint 1

JSON.parse(File.read(file)) reads and parses JSON in one step

Hint 2

Use hash.dig('key1', 'key2', 0) for safe nested access

Hint 3

rescue JSON::ParserError catches invalid JSON

Hint 4

Check if value.nil? to detect missing fields

Hint 5

Arrays in JSON become Ruby arrays - access with index [0]

Hint 6

Nested hashes: data['main']['temp'] or data.dig('main', 'temp')

Hint 7

exit 1 indicates error, exit 0 indicates success

Provided Files (Read-only)

1. Valid weather data

Input:
parse_weather('weather.json')
Expected Output:
City: London
Temperature: 15.5°C
Condition: partly cloudy
Humidity: 72%

2. Different city

Input:
parse_weather('weather.json')
Expected Output:
City: Tokyo
Temperature: 22.3°C
Condition: clear sky
Humidity: 65%

3. Negative temperature

Input:
parse_weather('weather.json')
Expected Output:
City: Moscow
Temperature: -5.2°C
Condition: snowy
Humidity: 85%
+ 3 hidden test cases