0

I have the downloadable DynamoDB local(currently working). I have read their documents and their example code is working.

I created the Users table with the name "Users"

Here is the UsersCreateTable.html

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />

    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>

    <script>
AWS.config.update({
  region: "us-west-2",
  endpoint: 'http://localhost:8000/',
  // accessKeyId default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  accessKeyId: "fakeMyKeyId",
  // secretAccessKey default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  secretAccessKey: "fakeSecretAccessKey"
});

var dynamodb = new AWS.DynamoDB();

function createUsers() {
    var params = {
        TableName : "Users",
        KeySchema: [
            { AttributeName: "id", KeyType: "HASH"}
        ],
        AttributeDefinitions: [
            { AttributeName: "id", AttributeType: "N" }
        ],
        ProvisionedThroughput: {
            ReadCapacityUnits: 5,
            WriteCapacityUnits: 5
        }
    };

    dynamodb.createTable(params, function(err, data) {
        if (err) {
            document.getElementById('textarea').innerHTML = "Unable to create users table: " + "\n" + JSON.stringify(err, undefined, 2);
        } else {
            document.getElementById('textarea').innerHTML = "Created users table: " + "\n" + JSON.stringify(data, undefined, 2);
        }
    });
}

    </script>

    <title></title>
</head>
<body>

    <input id="createTableButton" type="button" value="Create Table" onclick="createUsers();" />
    <br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>


</body>
</html>

I have prepared the sample JSON data for our Users Table. I have modified their MoviesLoadTable.html file and converted it to UsersLoadTable.html which uploads JSON data to the DynamoDB local, in order to load my JSON Users Data.

When I try to load my JSON data I get these errors on console:

Uncaught SyntaxError: Unexpected token : in JSON at position 497
    at JSON.parse (<anonymous>)
    at FileReader.r.onload (UsersLoadData.html:31)
r.onload @ UsersLoadData.html:31
FileReader (async)
processFile @ UsersLoadData.html:53

And here is the UsersLoadData.html

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />

    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>

    <script type="text/javascript">
AWS.config.update({
  region: "us-west-2",
  endpoint: 'http://localhost:8000/',
  // accessKeyId default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  accessKeyId: "fakeMyKeyId",
  // secretAccessKey default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  secretAccessKey: "fakeSecretAccessKey"
});

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

function processFile(evt) {
    document.getElementById('textarea').innerHTML = "";
    document.getElementById('textarea').innerHTML += "Importing users into DynamoDB. Please wait..." + "\n";
    var file = evt.target.files[0];
    if (file) {
        var r = new FileReader();
        r.onload = function(e) {
            var contents = e.target.result;
            var allUsers = JSON.parse(contents);

            allUsers.forEach(function (user) {
                document.getElementById('textarea').innerHTML += "Processing user id: " + user.id + "\n";
                var params = {
                    TableName: "Users",
                    Item: {
                        "id": user.id,
                        "info": user.info
                    }
                };
                docClient.put(params, function (err, data) {
                    if (err) {
                        document.getElementById('textarea').innerHTML += "Unable to add user: " + count + user.id + "\n";
                        document.getElementById('textarea').innerHTML += "Error JSON: " + JSON.stringify(err) + "\n";
                    } else {
                        document.getElementById('textarea').innerHTML += "Loading succeeded(id): " + user.id + "\n";
                        textarea.scrollTop = textarea.scrollHeight;
                    }
                });
            });
    };
        r.readAsText(file);
    } else {
        alert("Could not read users data file");
    }
}

    </script>


    <title></title>
</head>
<body>

    <input type="file" id="fileinput" accept='application/json' />
    <br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>

    <script>
        document.getElementById('fileinput').addEventListener('change', processFile, false);
    </script>

</body>
</html>

I searched for the error and could not find a satisfying solution. Thank you for your help.

0

1 Answer 1

1

Your JSON file has invalid format: enter image description here

Here you have fixed JSON:

    [{
        "id": 1,
        "info": {
            "name": "John",
            "surname": "Smith",
            "city": "NY",
            "birthday": "26/07/1996",
            "activities": [
                "Basketball",
                "Cinema",
                "NightOut"
            ],
            "badges": [
                "Friendly Player",
                "Basketball Pro"
            ],
            "reviews": [
                "Came to event on time",
                "Good basketball player",
                "I didn' like him",
                "Didn't show up on time"
            ],
            "connections(id)": [
                2,
                3,
                4
            ],
            "events": [{
                    "place": "Some Place",
                    "date": "10/10/2017",
                    "time": "18:00",
                    "activity": "Basketball"
                },
                {
                    "place": "Another Place",
                    "date": "13/10/2017",
                    "time": "21:00",
                    "activity": "Cinema"
                },
                {
                    "place": "Third Place",
                    "date": "19/10/2017",
                    "time": "22:00",
                    "activity": "NightOut"
                }
            ]
        }
    },

    {
        "id": 2,
        "info": {
            "name": "Adam",
            "surname": "Williams",
            "city": "San Francisco",
            "birthday": "Unknown",
            "activities": [
                "Tennis",
                "NightOut"
            ],
            "badges": [
                "Friendly Player",
                "Tennis Pro"
            ],
            "reviews": [
                "Adam is the best",
                "Best tennis player ever",
                "Don't play tennis with this guy"
            ],
            "connections(id)": [
                1,
                3,
                4
            ],
            "events": [{
                    "place": "Tennis Place",
                    "date": "01/03/2018",
                    "time": "20:00",
                    "activity": "Tennis"
                },
                {
                    "place": "Nightout Place",
                    "date": "03/03/2018",
                    "time": "20:00",
                    "activity": "NightOut"
                }
            ]
        }
    },

    {
        "id": 3,
        "info": {
            "name": "Juan",
            "surname": "Martinez",
            "city": "New Mexico",
            "birthday": "Unknown",
            "activities": [
                "Basketball",
                "NightOut"
            ],
            "badges": [
                "Pro Basketballer",
                "Night Owl"
            ],
            "reviews": [
                "Juan is crazy",
                "This guy can drink more than an elephant"
            ],
            "connections(id)": [
                1,
                2,
                4
            ],
            "events": [{
                    "place": "Basketball Court",
                    "date": "25/02/2018",
                    "time": "16:00",
                    "activity": "Basketball"
                },
                {
                    "place": "Nighclub",
                    "date": "03/03/2018",
                    "time": "19:00",
                    "activity": "NightOut"
                }
            ]
        }
    },

    {
        "id": 4,
        "info": {
            "name": "Charles",
            "surname": "Jackson",
            "city": "Pennsylvania",
            "birthday": "Unknown",
            "activities": [
                "Coding",
                "Lecture"
            ],
            "badges": [
                "Lecturer of Lecturers",
                "Code Master"
            ],
            "reviews": [
                "Best lecturer in the world",
                "Codes are amazing"
            ],
            "connections(id)": [
                1,
                2,
                3
            ],
            "events": [{
                    "place": "Pennsylvania University",
                    "date": "05/03/2018",
                    "time": "13:00",
                    "activity": "Lecture"
                },
                {
                    "place": "Pennsylvania University",
                    "date": "05/03/2018",
                    "time": "16:00",
                    "activity": "Coding"
                }
            ]
        }
    }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Remove "event 1", "event 2", "event 3" keys from arrays. Fixed json you have above

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.