CSV and JSON files · 13 / 15 Exercise Medium

Most populated city

Objective

Create a Python program that analyzes a JSON with nested data.

Instructions

  • Read a line with a JSON object with the keys country (text) and cities, a list of objects with name and population (integer).
  • Convert it with json.loads.
  • On the first line show Most populated: city (population). If there is a tie, the first one in the list.
  • On the second line show Total population: T, with the sum of the populations of all the cities.
Input
{"country": "España", "cities": [{"name": "Madrid", "population": 3300000}, {"name": "Sevilla", "population": 680000}, {"name": "Bilbao", "population": 345000}]}
Output
Most populated: Madrid (3300000)
Total population: 4325000

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise