jsonamazon-dynamodbcommand-line-interfaceaws-cloud9

Looking for solution to `Error parsing parameter '--request-items': Invalid JSON:...`


I'm trying to use AWS Cloud9 IDE in the CLI to create a table in AWS DynamoDB, then have a .json file filled with 20 items and use the CLI to upload those 20 items to the DynamoDB table with a variety of other attributes as well. I've created the table sucessfully but when I go to write the .json file to the table I recieved an error.

I made sure I was in the same directory as my .json file in the CLI in Cloud9. and ran

voclabs:~/environment/Homework2 $ aws dynamodb batch-write-item --request-items file://sensor_items.json

which returned

Error parsing parameter '--request-items': Invalid JSON:
[
 {
   "Sensor": {"S": "Sensor1"},
   "SensorDescription": {"S": "Description 1"},
   "ImageFile": {"S": "/Sensors/images/sensor1.jpg"},
   "SampleRate": {"N": 2048},
   "Locations": {"SS": ["Aberdeen MD", "Warren MI", "Orlando FL"]}
 },
 {
   "Sensor": {"S": "Sensor2"},
   "SensorDescription": {"S": "Description 2"},
   "ImageFile": {"S": "/Sensors/images/sensor2.jpg"},
   "SampleRate": {"N": 4096},
   "Locations": {"SS": ["New York NY", "Chicago IL"]},
   "SensorFunctional": {"BOOL": true}
 },
 {
    "Sensor": {"S": "Sensor3"},
    "ImageFile": {"S": "/Sensors/images/sensor3.jpg"},
    "SampleRate": {"N": 2829},
    "Locations": {"SS": ["San Fransisco, CA", "Las Vegas, NV"]}
 },....

I'm thinking that maybe my formatting is incorrect in my .json file but I haven't found a solution yet.


Solution

  • Your JSON does not match the payload expected by a BatchWriteItem:

    {
       "RequestItems": { 
          "TableName" : [ 
             { 
                "PutRequest": { 
                   "Item": { 
                      "string" : { 
                         "B": blob,
                         "BOOL": boolean,
                         "BS": [ blob ],
                         "L": [ 
                            "AttributeValue"
                         ],
                         "M": { 
                            "string" : "AttributeValue"
                         },
                         "N": "string",
                         "NS": [ "string" ],
                         "NULL": boolean,
                         "S": "string",
                         "SS": [ "string" ]
                      }
                   }
                }
             }
          ]
       },
       "ReturnConsumedCapacity": "string",
       "ReturnItemCollectionMetrics": "string"
    }
    

    https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

    You have to have your JSON nested inside the table name along with the PutRequest parameter.