AWS DynamoDB snippets

How to populate an existent DynamoDB table with JSON data in Python boto3

Please note that this snippet is part of the DynamoDB-Simpsons-episodes-full-example repository on GitHub.

Let's say you want to read the data from a JSON file and insert them inside an existent DynamoDB table

First of all, you have to create a Client (follow these steps) and then you can use the following code to assign a value to the variables used below.

TABLENAME='simpsonsEpisodes'
PATH_FILE_READ='02.Clean data/dataset_DDB.json'

The next step is to define a specific function where you also have to map the fields of the JSON file and specify the name of the DynamoDB attributes and types.

def upload():
    with open(PATH_FILE_READ, 'r') as datafile:
        records = json.load(datafile)
    for row in records:
        print(row)
        item = {
            'No_Season':{'N':str(row['No. Season'])},
            'No_Inseason':{'N':str(row['No. inseason'])},
            'No_Overall':{'N': str(row['No.overall'])},
            'Title':{'S': row['Title']},
            'DirectedBy':{'SS': [x.strip() for x in row['Directed by'].replace(', ','&').split('&')]},
            'WrittenBy':{'SS': [x.strip() for x in row['Written by'].replace(', ','&').split('&')]},
            'OriginalAirDate':{'S': row['Original air date']},
            'ProdCode':{'S': row['Prod.code']},
            'USViewers(millions)':{'N': str(row['U.S. viewers(millions)'])},
            'No_Overall':{'N': str(row['No.overall'])}
        }
        print(item)
        response = dynamodbClient.put_item(
            TableName=TABLENAME, 
            Item=item
        )
        print("UPLOADING ITEM")
        print(response)

Now it is time to execute the function

upload()

and read the output of the execution:

{'No.overall': 204, 'No. inseason': 1, 'Title': 'Lard of the Dance', 'Directed by': 'Dominic Polcino', 'Written by': "Jane O'Brien", 'Original air date': '1998-08-23', 'Prod.code': '5F20', 'U.S. viewers(millions)': 7.0, 'No. Season': 10}
    {'No_Season': {'N': '10'}, 'No_Inseason': {'N': '1'}, 'No_Overall': {'N': '204'}, 'Title': {'S': 'Lard of the Dance'}, 'DirectedBy': {'SS': ['Dominic Polcino']}, 'WrittenBy': {'SS': ["Jane O'Brien"]}, 'OriginalAirDate': {'S': '1998-08-23'}, 'ProdCode': {'S': '5F20'}, 'USViewers(millions)': {'N': '7.0'}}
    UPLOADING ITEM
    {'ResponseMetadata': {'RequestId': 'GUDG2H19G81HU8QGOT9KKOVM5FVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 07 Dec 2022 16:20:15 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '2', 'connection': 'keep-alive', 'x-amzn-requestid': 'GUDG2H19G81HU8QGOT9KKOVM5FVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '2745614147'}, 'RetryAttempts': 0}}
    {'No.overall': 205, 'No. inseason': 2, 'Title': 'The Wizard of Evergreen Terrace', 'Directed by': 'Mark Kirkland', 'Written by': 'John Swartzwelder', 'Original air date': '1998-09-20', 'Prod.code': '5F21', 'U.S. viewers(millions)': 7.95, 'No. Season': 10}
    {'No_Season': {'N': '10'}, 'No_Inseason': {'N': '2'}, 'No_Overall': {'N': '205'}, 'Title': {'S': 'The Wizard of Evergreen Terrace'}, 'DirectedBy': {'SS': ['Mark Kirkland']}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'OriginalAirDate': {'S': '1998-09-20'}, 'ProdCode': {'S': '5F21'}, 'USViewers(millions)': {'N': '7.95'}}
    UPLOADING ITEM
    {'ResponseMetadata': {'RequestId': '8NQUNNJDT1G1CM27RH6G555MAVVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 07 Dec 2022 16:20:16 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '8NQUNNJDT1G1CM27RH6G555MAVVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '2745614147'}, 'RetryAttempts': 0}}
    {'No.overall': 206, 'No. inseason': 3, 'Title': 'Bart the Mother', 'Directed by': 'Steven Dean Moore', 'Written by': 'David X. Cohen', 'Original air date': '1998-09-27', 'Prod.code': '5F22', 'U.S. viewers(millions)': 7.35, 'No. Season': 10}
    {'No_Season': {'N': '10'}, 'No_Inseason': {'N': '3'}, 'No_Overall': {'N': '206'}, 'Title': {'S': 'Bart the Mother'}, 'DirectedBy': {'SS': ['Steven Dean Moore']}, 'WrittenBy': {'SS': ['David X. Cohen']}, 'OriginalAirDate': {'S': '1998-09-27'}, 'ProdCode': {'S': '5F22'}, 'USViewers(millions)': {'N': '7.35'}}
    UPLOADING ITEM
    {'ResponseMetadata': {'RequestId': 'H3ADS7I2EQS4DBP9ASG5CMI2NRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 07 Dec 2022 16:20:16 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '2', 'connection': 'keep-alive', 'x-amzn-requestid': 'H3ADS7I2EQS4DBP9ASG5CMI2NRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '2745614147'}, 'RetryAttempts': 0}}
    {'No.overall': 207, 'No. inseason': 4, 'Title': 'Treehouse of Horror IX', 'Directed by': 'Steven Dean Moore', 'Written by': 'Donick CaryLarry DoyleDavid X. Cohen', 'Original air date': '1998-10-25', 'Prod.code': 'AABF01', 'U.S. viewers(millions)': 8.5, 'No. Season': 10}
    {'No_Season': {'N': '10'}, 'No_Inseason': {'N': '4'}, 'No_Overall': {'N': '207'}, 'Title': {'S': 'Treehouse of Horror IX'}, 'DirectedBy': {'SS': ['Steven Dean Moore']}, 'WrittenBy': {'SS': ['Donick CaryLarry DoyleDavid X. Cohen']}, 'OriginalAirDate': {'S': '1998-10-25'}, 'ProdCode': {'S': 'AABF01'}, 'USViewers(millions)': {'N': '8.5'}}

Back to AWS DynamoDB cookbook page