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 (as provided)
    • 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. Return true on success, false on error

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')

Ruby 3.4

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. Returns true on success

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

3. Extracts nested temperature

Input:
parse_weather('weather.json')
puts 'Temp OK'
Expected Output:
City: London
Temperature: 15.5°C
Condition: partly cloudy
Humidity: 72%
Temp OK
+ 2 hidden test cases