3

I would like to batch upload a json file to dynamodb. At the moment I can successfully manually put items in a python file (as below) and upload to a table, however how can I amend the script to read an external json file (containing 200 items) and batch upload all 200 items to the table.

import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('exampletable')

with table.batch_writer() as batch:
    batch.put_item(
        Item={
            'ID': '2',
            'DateTime': '21/12/2017 13:16',
            'SourceDevice': '10',
            'DestinationDevice': '20',
            'DataType': 'full',
            'Activity': 'unusual'

    }
)
    batch.put_item(
        Item={
            'ID': '3',
            'DateTime': '21/12/2017 13:40',
            'SourceDevice': '10',
            'DestinationDevice': '20',
            'DataType': 'full',
            'Activity': 'unusual'

    }
)

json file contents as below

[{
"ID": "1",
"DateTime": "21/12/2017 13:16",
"SourceDevice": "10",
"DestinationDevice": "20",
"DataType": "part",
"Activity": "normal"
}, {
"ID": "1",
"DateTime": "21/12/2017 13:16",
"SourceDevice": "40",
"DestinationDevice": "25",
"DataType": "full",
"Activity": "unusual"
}]
1
  • Underrated question. Commented Mar 11, 2020 at 23:53

1 Answer 1

5

You would simply break that down into two tasks:

  1. Reading the JSON from a file
  2. Looping through the JSON, calling batch.put_item each time.

There are lots of results when you do a search for the first task. And the second task is literally just writing a loop.

A full solution would look something like this:

import json
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('exampletable')

# Read the JSON file
with open('items.json') as json_data:
    items = json.load(json_data)

    with table.batch_writer() as batch:

        # Loop through the JSON objects
        for item in items:
            batch.put_item(Item=item)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks I was way overthinking this using boto3 and getting myself confused. Your answer worked perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.