1

I want to import data from my JSON file into DynamoDB with this code:

var AWS = require("aws-sdk");
var fs = require('fs');

AWS.config.update({
    region: "us-west-2",
    endpoint: "http://localhost:8000"
});

var docClient = new AWS.DynamoDB.DocumentClient();

console.log("Importing Pays into DynamoDB. Please wait.");

var allPays = JSON.parse(fs.readFileSync('paysdata.JSON', 'utf8'));
allPays.forEach(function(pays) {
    var params = {
        TableName: "Pays",
        Item: {
            "region":  pays.region,
            "name": pays.name,
            "name.common": pays.name.common,
            "languages":  pays.languages,
            "area": pays.area
        }
    };

    docClient.put(params, function(err, data) {
       if (err) {
           console.error("Unable to add Pays", pays.name.common, ". Error JSON:", JSON.stringify(err, null, 2));
       } else {
           console.log("PutItem succeeded:", pays.name.common);
       }
    });
});

But I still get this error HERE Does anyone have an idea on how to fix this problem? I tried to follow the same steps on the site: Create table using nodejs Dynamodb?

But it does not work.

My CreateTable.js

var AWS = require("aws-sdk");

AWS.config.update({
  region: "us-west-2",
  endpoint: "http://localhost:8000"
});

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "Pays",
    KeySchema: [       
        { AttributeName: "region", KeyType: "HASH"},  //Partition key
        { AttributeName: "name", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "region", AttributeType: "S" },
        { AttributeName: "name", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

Some data :

[
    {
        "name": {
            "common": "Aruba",
            "official": "Aruba",
            "native": {
                "nld": {
                    "official": "Aruba",
                    "common": "Aruba"
                },
                "pap": {
                    "official": "Aruba",
                    "common": "Aruba"
                }
            }
        },
        "translations": {
            "ces": {
                "official": "Aruba",
                "common": "Aruba"
            },
            "deu": {
                "official": "Aruba",
                "common": "Aruba"
            },
            "jpn": {
                "official": "\u30a2\u30eb\u30d0",
                "common": "\u30a2\u30eb\u30d0"
            },
            "kor": {
                "official": "\uc544\ub8e8\ubc14",
                "common": "\uc544\ub8e8\ubc14"
            },

1 Answer 1

1
        "name": {
            "common": "Aruba",
            "official": "Aruba",
            "native": {
                "nld": {
                    "official": "Aruba",
                    "common": "Aruba"
                },
                "pap": {
                    "official": "Aruba",
                    "common": "Aruba"
                }
            }
        }

name is an object so you cannot pays.name as the value for name, since it is of "String" datatype.

To fix the problem, you can change the table definition to:

var params = {
    TableName : "Pays",
    KeySchema: [       
        { AttributeName: "region", KeyType: "HASH"},  //Partition key
        { AttributeName: "name.common", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "region", AttributeType: "S" },
        { AttributeName: "name.common", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};
Sign up to request clarification or add additional context in comments.

4 Comments

0 I tried to add the item "name" but i have this result : Unable to add Pays Zimbabwe . Error JSON: { "message": "Invalid attribute value type", "code": "ValidationException", "time": "2022-01-15T12:31:39.062Z", "requestId": "7b5937f7-b52d-4f5b-8f6a-9792811a231f", "statusCode": 400, "retryable": false, "retryDelay": 23.159447753247807
What is the data type of pays.name.common and pays.region? It needs to be a String for both because you have specified an attribute type of "S" in the table definition.
I edited my post and copied some data
I have updated my answer based on your edits

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.