coderain blog

How to Import JSON File to Load DynamoDB Table in AWS: Console Method or AWS SDK Required?

Amazon DynamoDB is a fully managed NoSQL database service known for its scalability, high performance, and low latency. Whether you’re migrating data from another system, loading initial datasets, or restoring backups, importing data into DynamoDB is a common task. JSON (JavaScript Object Notation) is a popular format for data exchange due to its readability and flexibility, making it a top choice for such imports.

But how do you actually import a JSON file into DynamoDB? AWS offers two primary approaches:

  1. Console Method: Using the AWS Management Console (no code required, leverages Amazon S3 for storage).
  2. AWS SDK Method: Writing code with the AWS SDK (e.g., Python, JavaScript) for programmatic control.

This blog will guide you through both methods, their prerequisites, step-by-step workflows, and help you decide which approach fits your use case.

2026-01

Table of Contents#

  1. Prerequisites
  2. Method 1: Import via AWS Management Console (Using S3)
    • 2.1 Prepare Your JSON File
    • 2.2 Upload JSON to Amazon S3
    • 2.3 Create a DynamoDB Import Job
    • 2.4 Monitor the Import Job
  3. Method 2: Import via AWS SDK (Programmatic Approach)
    • 3.1 AWS SDK Setup
    • 3.2 Python Example (Boto3)
    • 3.3 Node.js Example (AWS SDK for JavaScript)
  4. Console vs. SDK: When to Use Which?
  5. Troubleshooting Common Issues
  6. Conclusion
  7. References

Prerequisites#

Before starting, ensure you have the following:

  • AWS Account: With administrative access or permissions for DynamoDB, S3, and IAM (for console method).
  • DynamoDB Table: An existing table with the correct schema (primary key and sort key, if applicable). Imports will fail if the table doesn’t exist or has mismatched keys.
  • JSON File: Data in DynamoDB-compatible JSON format (see Section 2.1 for details).
  • S3 Bucket (for console method): A bucket to store the JSON file (must be in the same AWS Region as your DynamoDB table).
  • AWS SDK Setup (for SDK method): Install the AWS SDK for your preferred language (e.g., boto3 for Python, aws-sdk for Node.js) and configure credentials (via aws configure or IAM roles).

Method 1: Import via AWS Management Console (Using S3)#

The DynamoDB console supports importing data directly from Amazon S3, making it ideal for large datasets (up to terabytes) with minimal coding. Here’s how:

2.1 Prepare Your JSON File#

DynamoDB requires JSON data in JSON Lines format (also called “newline-delimited JSON”), where each line is a standalone JSON object representing a DynamoDB item. Do not use a single JSON array (e.g., [{...}, {...}]).

Example: Correct JSON Lines Format#

{"id": {"S": "1"}, "name": {"S": "Alice"}, "age": {"N": "30"}}
{"id": {"S": "2"}, "name": {"S": "Bob"}, "age": {"N": "25"}}

Example: Incorrect Format (Avoid This!)#

[
  {"id": "1", "name": "Alice", "age": 30},
  {"id": "2", "name": "Bob", "age": 25}
]
  • Why? DynamoDB imports parse one item per line. Arrays or multi-line JSON objects will cause errors.
  • Data Types: Each attribute must specify its DynamoDB data type (e.g., {"S": "string"}, {"N": "number"}, {"BOOL": true}). See DynamoDB Data Types for details.

2.2 Upload JSON to Amazon S3#

  1. Go to the S3 Console.
  2. Select your bucket (or create a new one in the same Region as your DynamoDB table).
  3. Click Upload, then drag-and-drop your JSON file or select it from your local machine.
  4. Click Upload to confirm.

2.3 Create a DynamoDB Import Job#

  1. Go to the DynamoDB Console.
  2. In the left menu, select Tables, then choose your target table.
  3. Go to the Actions dropdown and select Import from S3.
  4. On the “Import from S3” page:
    • S3 URL: Enter the path to your JSON file (e.g., s3://my-bucket/dynamodb-import/data.json).
    • Import mode: Choose “Overwrite” (replace existing items) or “Append” (add new items).
    • IAM role: Select an existing role with permissions for s3:GetObject (to read from S3) and dynamodb:ImportTable (to write to DynamoDB). If no role exists, create one using the AWS IAM console with these permissions.
  5. Click Import to start the job.

2.4 Monitor the Import Job#

  • Track progress in the DynamoDB console under Imports (left menu).
  • Statuses: IN_PROGRESS, COMPLETED, FAILED.
  • For failed jobs, check the Failure reason (e.g., invalid JSON, missing permissions, mismatched primary keys).

Method 2: Import via AWS SDK (Programmatic Approach)#

The AWS SDK is ideal for small-to-medium datasets, programmatic workflows, or integrating data imports into scripts. We’ll use Python (boto3) and Node.js (AWS SDK for JavaScript) as examples.

3.1 AWS SDK Setup#

Python (boto3)#

pip install boto3
aws configure  # Enter your AWS access key, secret key, Region, and output format

Node.js#

npm install aws-sdk
# Configure credentials via environment variables or ~/.aws/credentials

3.2 Python Example (Boto3)#

This example uses BatchWriteItem to import items from a JSON file. BatchWriteItem allows writing up to 25 items per request (16MB limit), making it efficient for small datasets. For larger data, add pagination or use DynamoDBMapper (a higher-level library).

Step 1: Prepare the JSON File#

Use the same JSON Lines format as in the console method (each line is a DynamoDB item).

Step 2: Python Script#

import boto3
import json
 
# Initialize DynamoDB client
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table('YourTableName')
 
def import_json_to_dynamodb(json_file_path):
    with open(json_file_path, 'r') as f:
        for line in f:
            item = json.loads(line)  # Parse each line as a DynamoDB item
            # Use BatchWriteItem (for bulk imports) or put_item (for single items)
            table.put_item(Item=item)
            print(f"Added item: {item['id']}")
 
if __name__ == "__main__":
    import_json_to_dynamodb('data.json')

Notes:#

  • Error Handling: Add retries for ProvisionedThroughputExceededException using botocore.retryhandler.
  • BatchWriteItem: For bulk imports, group items into batches of 25:
    from boto3.dynamodb.types import TypeSerializer
     
    def batch_write(items):
        serializer = TypeSerializer()
        batch = [{'PutRequest': {'Item': serializer.serialize(item)}} for item in items]
        response = table.batch_writer().put_item(Item=item)  # Simplified batch writer

3.3 Node.js Example (AWS SDK for JavaScript)#

This example uses batchWriteItem to import items.

const AWS = require('aws-sdk');
const fs = require('fs');
 
AWS.config.update({ region: 'us-east-1' });
const dynamodb = new AWS.DynamoDB.DocumentClient();
 
async function importJsonToDynamoDB(filePath) {
  const items = fs.readFileSync(filePath, 'utf8').split('\n').filter(line => line);
  
  for (const line of items) {
    const item = JSON.parse(line);
    const params = {
      TableName: 'YourTableName',
      Item: item
    };
    await dynamodb.put(params).promise();
    console.log(`Added item: ${item.id}`);
  }
}
 
importJsonToDynamoDB('data.json').catch(console.error);

Console vs. SDK: When to Use Which?#

FactorConsole (S3 Import)AWS SDK
Dataset SizeLarge (TBs)Small-to-medium (GBs or less)
Coding RequiredNoYes (Python, Node.js, etc.)
FlexibilityLimited (point-and-click)High (custom logic, retries, validation)
DependenciesRequires S3 bucketNone (direct API calls)
Use CasesOne-time imports, large backupsScripts, CI/CD pipelines, dynamic data

Troubleshooting Common Issues#

  • Invalid JSON Format: Ensure JSON uses JSON Lines (one item per line) and correct DynamoDB data types (e.g., {"S": "string"}).
  • S3 Permissions: The IAM role for console imports must allow s3:GetObject on the S3 bucket.
  • Throttling: SDK imports may hit ProvisionedThroughputExceededException. Retry with exponential backoff or increase table throughput.
  • Mismatched Primary Keys: Imported items must match the table’s primary key schema (e.g., missing id if id is the primary key).

Conclusion#

Importing JSON into DynamoDB is straightforward with either the AWS Console (for large, no-code imports via S3) or the AWS SDK (for programmatic control). Choose the console for simplicity and scale, and the SDK for flexibility and integration into scripts. Always validate your JSON format and test with a small dataset first!

References#