Learn to Code via Tutorials on Repl.it

← Back to all posts
Fetching and processing a JSON file using Python
freddiethefrog (59)

Fetching and processing a JSON file using Python

Part 1: Displaying the quotes

We will use the Python requests module to fetch a JSON file that contains wise quotes. We will then convert the JSON to a list, shuffle it and then use a for loop to display the quotes. Once we have the quotes displayed we will colour them using the termcolor module.

Here is the start of the JSON data source.

[
    {
      "author": "Abraham Lincoln",
      "quote": "Things may come to those who wait, but only the things left by those who hustle.",
      "image": "http://upload.wikimedia.org/wikipedia/commons/1/1b/Abraham_Lincoln_November_1863.jpg"
    },
    {
      "author": "Adam Smith",
      "quote": "The great secret of education is to direct vanity to proper objects.",
      "image": "https://thenypost.files.wordpress.com/2014/10/adam-smith.jpg"
    },
... ]

First we need to import some modules. Note that we import replit so we can use its clear method to clear the console.

import requests, random, replit

Next we need to use the requests module's get method to fetch the external JSON resource. response will be a requests Response type object.

json_url = 'https://gist.githubusercontent.com/shakked/2a964ccf120b6a853786/raw/bda7928fe658a847506a3e564d37e9ae353cba61/quotes.json'
response = requests.get(json_url)

Now we will convert the response into a Python list using the requests json method. We will store tje list using the quotes variable.

quotes = response.json()

Next we will use the shuffle method from the random module to shuffle the list. Then we clear the console using the replit module's clear method.

random.shuffle(quotes)
replit.clear()

We now use a for loop to display the quotes. Each one consists of a dictionary that has three key/value pairs. The keys are: author , quote and image . This last one contains the URL associated with the quote. Because we are displaying in the terminal we can not display the image. Note how each quote has a string of 60 asterisks as a border at the bottom, then a new line.

num_quotes = len(quotes)
for index in range(num_quotes):
  quote = quotes[index]
  author_string = "Author: " + quote["author"]
  print(author_string)  
  print(Quote: " + quote["quote"])
  print("*" * 60, "\n")

Part 2: Colouring the quotes

The termcolor module's color method allows us to colour terminal output. Lets use it. Edit the for loop code so it is the same as that below.

for index in range(num_quotes):
  quote = quotes[index]
  author_string = colored("Author: " + quote["author"], "white", "on_red", attrs=['bold'])
  print(str(index + 1) +")", author_string)
  
  print(colored("Quote: " + quote["quote"], "white", "on_magenta", attrs=['bold']))
  print("*" * 60, "\n")

For a reference about how to use color method visit this link: https://kite.com/python/docs/termcolor.colored

For a more advanced version of the program, that asks the user for the number of quotes they want to see, examine this program: https://repl.it/@freddiethefrog/python-wise-sayings