Step 2 - Looping over JSON

Looping over Dictionaries and JSON

As we looked at in the Inputs section, JSON is a common format used to exchange information between computers, programs, functions and Application Programming Interfaces (APIs). JSON shares lots of similarities with a python dictionary. If you need a reminder of the differences review the Inputs section of the workshop.

In this section, we are going to look at a common use case where our input data is a sequence of dictionaries inside a list. In the first part of this section, you learned how to loop over a list. We can apply the same principle when the list comprises dictionaries.

  • In the inputs lesson, we created a JSON file called translate_input.json. Edit this file or create a new file to contain the following.
{
    "Input":[
        {
        "Text":"What is cloud computing?",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        },
        {
        "Text":"Cloud computing is the on-demand delivery of IT resources over the Internet with pay-as-you-go pricing.",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        },
        {
        "Text":"Instead of buying, owning, and maintaining physical data centers and servers, you can access technology services, such as computing power, storage, and databases, on an as-needed basis from a cloud provider like Amazon Web Services (AWS)",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        },
        {       
        "Text":"Who is using cloud computing?",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        },
        {       
        "Text":"Organizations of every type, size, and industry are using the cloud for a wide variety of use cases, such as data backup, disaster recovery, email, virtual desktops, software development and testing, big data analytics, and customer-facing web applications.",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        },
        {       
        "Text":"For example, healthcare companies are using the cloud to develop more personalized treatments for patients. Financial services companies are using the cloud to power real-time fraud detection and prevention.",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        },        
        {       
        "Text":"And video game makers are using the cloud to deliver online games to millions of players around the world.",
        "SourceLanguageCode":"en",
        "TargetLanguageCode":"fr"
        }
    ] 
} 
  • Create a new python file called lab_6_step_2_loops.py and add the text below:
# Standard Imports
import argparse
import json

# 3rd Party Imports
import boto3 

# Arguments
parser = argparse.ArgumentParser(description="Provides translation  between one source language and another of the same set of languages.")
parser.add_argument(
    '--file',
    dest='filename',
    help="The path to the input file. The file should be valid json",
    required=True)

args = parser.parse_args()

# Functions
def open_input():
    """This function returns a dictionary containing the contents of the Input section in the input file""" 
    with open(args.filename) as file_object:
        contents = json.load(file_object)
    return contents['Input']

# Boto3 function to use Amazon Translate to translate the text and only return the Translated Text
def translate_text(**kwargs): 
    client = boto3.client('translate')
    response = client.translate_text(**kwargs)
    print(response['TranslatedText']) 

# Add a Loop to iterate over the json file.
def translate_loop():
    input_text = open_input()
    for item in input_text: # Here we iterate over all dictionaries in the Input list
        translate_text(**item)

# Main Function - use to call other functions
def main():
    translate_loop()

if __name__ == "__main__":
    main()
  • To run the program, enter the following command in the terminal:

    python lab_6_step_2_loops.py --file translate_input.json

This should return the following:

Qu'est-ce que le cloud computing ?
L'informatique en nuage est la fourniture à la demande de ressources informatiques sur Internet avec un tarif à l'utilisation.
Au lieu d'acheter, de posséder et de gérer des centres de données physiques et des serveurs, vous pouvez accéder aux services technologiques, tels que la puissance de calcul, le stockage et les bases de données, au besoin auprès d'un fournisseur de cloud tel qu'Amazon Web Services (AWS)
Qui utilise le cloud computing ?
Les entreprises de tous les types, tailles et secteurs utilisent le cloud pour une grande variété de cas d'utilisation, tels que la sauvegarde des données, la reprise après sinistre, la messagerie électronique, les postes de travail virtuels, le développement et les tests de logiciels, l'analyse du Big Data et les applications Web orientées vers le client.
Par exemple, les entreprises de soins de santé utilisent le cloud pour développer des traitements plus personnalisés pour les patients. Les entreprises de services financiers utilisent le cloud pour détecter et prévenir les fraudes en temps réel.
Et les fabricants de jeux vidéo utilisent le cloud pour offrir des jeux en ligne à des millions de joueurs à travers le monde.

What did we do?

  • We created a file called translate_input.json which contained a list [] with nested dictionaries {}.
  • We added an additional function called translate_loop(). This called the open_input() function and stored the JSON from the input.json file in a variable called input_text.
  • We amended the translate_text() function to only return the TranslatedText using print(response['TranslatedText']).

What did python do?

  • The function translate_loop() contains a for loop which take each dictionary {} item from the list [] and then calls the translate_text() function using the value stored in a variable item as an input parameter.
  • The loop iterates over each item in the list calling translate_text() each time.