Deliverables

Inside json-quiz/ will be five files, named and numbered as 1.py, 2.py, etc, each corresponding to the problems listed below.

Each problem has a list of tasks (lettered A through G, or so) and the expected output. When you run each script, it should generate the exact output as expected, i.e. you will be using print() calls.

Some of the problems have partial solutions. The first problem is completely solved for you as an example. Copy that into your repo (as json-quiz/1.py) and run it and see that you get the expected output.

Quick intro to JSON, Dicts, Lists, etc.

Understanding how text turns into data structures is pretty much fundamental to doing anything useful in programming, especially in the journalism/scientific domain.

Lists are how ordered collections of objects (numbers, strings, other data objects) are represented:

[1,2,3,'bingo']

And Dictionaries are how Python represents objects with attributes, i.e. a car is an "object" and that "object" has attributes such as color, mpg, and make:

{"color":"red","mpg":20.5,"make":"Honda"}

In other words, lists and dictionaries can be used to describe pretty much every real-world object there is. Hence, a lot of programming involves turning real-life data objects into lists and dictionaries, so that they can be processed with code. And that's why we learn these data structures.

Why JSON

While there are several different textual data formats, JSON is the most ubiquitous among modern services, and the most versatile. And just as importantly, in Python (as well as Ruby and JavaScript), the dict (dictionary) and list structures look the same in code as they do in JSON text files.

From Python data object to JSON

For instance, this is a Python dictionary object:

d={"apples":20,"pears":40,"kiwis":90}print(d['pears'])# 40

This is how you write that data structure into a JSON formatted text file, which is referred to as, encoding the data object as JSON.

From JSON to Python data object

A more frequent situation is that you'll have data in a JSON text file and you want to bring it into your Python program. This is referred to as parsing the JSON text file, or: decoding the JSON text file.

If we want to write a program that tells us if Github's status is good or not (i.e. a istheLtrainf—ed.com, but for Github), we have to:

Fetch the text file from the URL

Use Python's JSON parser to convert that text into a Python data object (in this case, a dict)

Here's one way to do it:

First, we have to get the contents of that URL (https://status.github.com/api/status.json) as a text string:

importrequestsimportjsonxfile=requests.get("https://status.github.com/api/status.json")# xfile is not a text file, but a Requests object, so # we need this intermediary step:txt=xfile.textprint(txt)# {"status":"good","last_updated":"2015-04-13T16:36:48Z"}

Data URL

Tasks

Note that the top-level object contains a list of results; this is because for any given address-lookup, the address given may have been vague enough to require more than one result (see the result for "Paris". For this problem, though, results contains exactly one object, which I refer to as the "result object".

A. Print the value of the formatted_address attribute for the result object.

B. For the top-level object, print out the value of the status attribute.

C. For the result object, print out the location_type, which is part of the geometry object.

D. Print the value of the lat attribute of the location object that is part of the geometry object (which is, again, part of the result object)

E. Again, inside the geometry of the result object, print the value of the southwestlng attribute, which is inside the geometry's viewport object.

F. For the result object, print the long_name values for the first 2address_components, joined (by a commma-and-space).

Previously we’ve used genre and artist tagging from AllMusic for related artists which worked well, but did not cover a large portion of our catalogue. What we’ve done now is to go through months and months of listening data and look closely at what people listen to.

This allows us to see that users who listen to a lot of The Rolling Stones, for example, are also big fans of Iggy Pop or The Byrds. The new feature pulls some of this information together to show you a range of related artists in one tab.

Partial answer

importrequestsimportjsondata_url='http://www.compjour.org/files/code/json-examples/single-tweet-librarycongress.json'data=json.loads(requests.get(data_url).text)### For G.hashtag_objs=data['entities']['hashtags']hashtag_texts=[]forhinhashtag_objs:hashtag_texts.append(h['text'])print('G.',','.join(hashtag_texts))# alternatively, you could also use the list comprehension syntax:# hashtag_texts = [h['text'] for h in data['entities']['hashtags']]

3.

importrequestsimportjsondata_url="http://www.compjour.org/files/code/json-examples/maps.googleapis-geocode-mcclatchy.json"response=requests.get(data_url)data=json.loads(response.text)obj=data['results'][0]print('A.',obj['formatted_address'])print('B.',data['status'])print('C.',obj['geometry']['location_type'])print('D.',obj['geometry']['location']['lat'])print('E.',obj['geometry']['viewport']['southwest']['lng'])a=obj['address_components'][0]['long_name']b=obj['address_components'][1]['long_name']print('F.',a+', '+b)# or, to use list comprehensions:# print('F.', ', '.join(a['long_name'] for a in obj['address_components'][0:2]))

4.

importrequestsimportjsondata_url="http://www.compjour.org/files/code/json-examples/spotify-related-to-beyonce.json"data=json.loads(requests.get(data_url).text)artists=data['artists']print('A.',len(artists))print('B.',artists[4]['name'])print('C.',artists[11]['followers']['total'])print('D.',','.join(artists[0]['genres']))print('E.',artists[-1]['images'][0]['url'])# Note: the answer to E depends on that images array being sorted by size# ...if that weren't the case, we'd have to sort it like so:# from operator import itemgetter# images = sorted(artists[-1]['images'], key = itemgetter('width', 'height'))# print('E.', images[-1]['url'])

5.

importrequestsimportjsondata_url='http://www.compjour.org/files/code/json-examples/single-tweet-librarycongress.json'data=json.loads(requests.get(data_url).text)print('A.',data['created_at'])print('B.',data['user']['created_at'])print('C.',data['text'])print('D.',data['user']['screen_name'])print('E.',data['id'])print('F.',len(data['entities']['user_mentions']))### For G.hashtag_objs=data['entities']['hashtags']hashtag_texts=[]forhinhashtag_objs:hashtag_texts.append(h['text'])print('G.',','.join(hashtag_texts))# alternatively, you could also use the list comprehension syntax:# hashtag_texts = [h['text'] for h in data['entities']['hashtags']]### For Hurls=data['entities']['urls']urltxts=[]foruinurls:urltxts.append(u['display_url'])print('G.',','.join(urltxts))