Learn

We may find ourselves in situations where we are working with JSON Data. The JSON format is the preferred way to structure data such that it may be transmitted over the internet. An API call usually responds with data formatted in JSON. We can process JSON data using the higher-order functions we have been working with.

However, unlike a CSV file, we cannot use an iterator when we read data from a JSON file. The file’s entire contents are read and stored in a Python dictionary. If a JSON object contains another JSON object, the dictionary will contain another dictionary. Unfortunately, the json library in Python does not provide a method to produce an iterator that we can use to read data similar to a CSV; we will have to work with the dict type.

To illustrate this in practice, we will process data from the sample JSON file: cities.json, which contains the following data:

Click to see JSON data
{ "city": [ { "name": "New York", "country": "United States of America", "coordinates": [40.7128, -74.0060], "continent": "North America" }, { "name":"Los Angeles", "country": "United States of America", "coordinates": [34.0522, 118.2437], "continent": "North America" }, { "name": "Montreal", "country": "Canada", "coordinates": [45.5017, -73.5673], "continent": "North America" }, { "name": "Toronto", "country": "Canada", "coordinates": [43.6532, -79.3832], "continent": "North America" } ] }

To work with the JSON file, we must first be open it and read it as we did for a CSV file:

import json with open('cities.json') as json_file: cities = json.load(json_file)

In the JSON file, there is an object called city that consists of an array of JSON objects containing data relevant to a particular city. When the JSON file is read, it will produce a dictionary containing one entry: a map between the string “city” and a list of dictionaries that represent data relevant to each contained city. The dictionary structure will be:

{"city":[{"name": "name_of_city", "country": "country_of_city", "coordinates":[latitude, longitude], "continent":"continent_of_city"}...]}.

The data formatted this way is hard to read and susceptible to errors when using it. It would be better to use map() to store this data as a namedtuple.

import json from collections import namedtuple city = namedtuple("city", ["name", "country", "coordinates", "continent"]) with open('cities.json') as json_file: data = json.load(json_file) # cities is a dictionary with one entry: {"cities":[{"name": name, "country":country, "coordinates":[lat, long], "continent":continent}...] # Extract the collection of cities from the dictionary - this is a list of dictionaries. cities_list = data["cities"] cities = map(lambda x: city(x["name"], x["country"], x["coordinates"], x["continent"]), cities_list)

The cities iterable can now store the entries of the JSON file in a much more readable way.

The other higher-order functions can be used in the same way as map() with cities now being the supplied iterable.

Instructions

1.

Store all cities that are on the continent of Asia in the tuple called asia. Make sure to print out your result.

2.

Find the western-most city in the asia tuple and store it in the west variable. Make sure to print out your result.

Note: the western-most country is the one where the longitude is the lowest number (minimum of city.coordinates[1])

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?