Open In App

How to Upload JSON File to Amazon DynamoDB using Python?

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It offers scalability, reliability, and fast access to data for applications of any scale. DynamoDB supports both document and key-value data models and handles administrative tasks, allowing developers to focus on their applications.

Features Of DynamoDB

Steps To Create a DynamoDB Table

Step 1: Sign in to your AWS account and click on Services. To create a free tier AWS account refer to Amazon Web Services – Setting Up an AWS Account.



Step 2: Search for DynamoDB and click on Create Table.

DynamoDB Create Table

Step 3: Configure the DynamoDB table by providing the table name and partition key values



DynamoDB Create Table

Step 4: Click on Create Table after the configuration

Uploading Json Objects to AWS DynamoDB using Python

Here we will be using Visual Studio Code for developing the Python Code. The boto3 package is used in the below code. This package can be installed using ‘pip install boto3‘ from the terminal. Boto3 is the SDK in Python for interacting with AWS Services directly.

details.json

[
    {
        "id": "1",
        "first_name": "Mihir",
        "last_name": "Panchal",
        "email": "mihirpanchal5400@gmail.com"
    },
    {
        "id": "2",
        "first_name": "Geeks",
        "last_name": "ForGeeks",
        "email": "geeksforgeeks@gmail.com"
    }
]

Python Boto3 Library

The boto3 library is a Python library that provides an interface to Amazon Web Services (AWS) services, including Amazon DynamoDB. The put_item() method on the DynamoDB client is used to insert an item into a table. The Item parameter of the put_item() method is a dictionary that contains the key-value pairs for the item.




import boto3
import json
  
dynamodb = boto3.client('dynamodb')
  
with open('details.json') as f:
    data = json.load(f)
  
for item in data:
    response = dynamodb.put_item(
        TableName='GeeksForGeeks',
        Item={
            'ID': {'S': item['id']},
            'first_name': {'S': item['first_name']},
            'last_name': {'S': item['last_name']},
            'email': {'S': item['email']},
        }
    )
    print(response)

Output 

Source Code and Output

Source Code and Output

FAQs On Amazon DynamoDB Using Python

1.  How To Get Data From DynamoDB Using Python?

By using the function which is provided by using boto3 you can get the data.

2. What Are The Main Benefits Of Using Amazon DynamoDB?

There will be contiounus backup of your data and the data is replicated across the multi region.


Article Tags :