Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Reading JSON files
5. JSON files with numbers
Summary and Review

Instruction

Of course, strings aren't the only things you can store in a JSON file; there can be numbers or logical values as well. We could have data like this:

{
  "first_name": "Emma",
  "last_name": "Jones",
  "age": 43
}

When you load the data into memory, you'll notice that the age field is a number, and you can do computations and comparisons with it:

import json
with open('customer2.json', 'r') as file:
  customer = json.load(file)
  if customer['age'] >= 18:
    print('Customer is an adult')
  else:
    years_to_adult = 18 - customer['age']
    print('Customer is a minor and will be an adult in', years_to_adult, 'years.')

Exercise

People typically retire at the age of 67. Load the JSON file named customer2.json into a variable named customer2, and display the number of years remaining until the customer retires, or state that they’re already retired. For example:

The customer has 10 years to retirement.

If the customer is at least 67, display:

The customer has already retired.