Open In App

How to Upload JSON File to Amazon DynamoDB using Python?

Last Updated : 24 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • DynamoDB excels in scalability, automatically handling millions of requests per second and supporting read and write scalability. It partitions data across servers to ensure high throughput. The service provides low-latency access to data using SSD storage and in-memory caching, making it suitable for real-time applications.
  • The database offers flexible data models. In the key-value model, data is stored and retrieved using a primary key. The document model allows nesting attributes within items and performing complex queries on the document structure.
  • ACID transactions are supported, ensuring atomicity and consistency for multiple operations. Security features include encryption at rest and in transit, fine-grained access control, and auditing capabilities.
  • DynamoDB’s Global Tables enable multi-region replication for high availability and disaster recovery. Integration with AWS services like Lambda, AppSync, Kinesis, and Glue streamlines application development.
  • With its versatility, scalability, and managed nature, DynamoDB is widely used in web applications, gaming, mobile apps, IoT, ad tech, and more. Developers appreciate its ability to handle large workloads, provide low-latency access, and simplify database management.

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

DynamoDB Create Table

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

DynamoDB Table Configuration

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.

Python3




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

DynamoDB 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads