The answer from E.J. Brennan looks correct, for a single record, but it doesn't answer the original question (which needs to add an array of records).
For this, the command is
aws dynamodb batch-write-item --request-items file://aws-requests.json
But, you'll need to make a modified JSON file, like so (note the DynamoDB JSON that specifies data types):
{
"YourTableName": [
{
"PutRequest": {
"Item": {
"Column1": { "S": "Column1 Value" },
"Column2": { "S": "Column2 Value" },
"Column3": { "S": "Column3 Value" },
"Column4": { "S": "Column4 Value" },
}
}
},
{
"PutRequest": {
"Item": {
"Column1": { "S": "Column1 Value" },
"Column2": { "S": "Column2 Value" },
"Column3": { "S": "Column3 Value" },
"Column4": { "S": "Column4 Value" },
}
}
}
]
}
Answer from carpiediem on Stack OverflowAWS - import JSON file to load Dynamo table
Write a JSON File as a DynamoDB Table Item - AWS - HashiCorp Discuss
Creating Amazon DynamoDB Table Items from a JSON File - Stack Overflow
Using DynamoDB as a JSON dump store?
The answer from E.J. Brennan looks correct, for a single record, but it doesn't answer the original question (which needs to add an array of records).
For this, the command is
aws dynamodb batch-write-item --request-items file://aws-requests.json
But, you'll need to make a modified JSON file, like so (note the DynamoDB JSON that specifies data types):
{
"YourTableName": [
{
"PutRequest": {
"Item": {
"Column1": { "S": "Column1 Value" },
"Column2": { "S": "Column2 Value" },
"Column3": { "S": "Column3 Value" },
"Column4": { "S": "Column4 Value" },
}
}
},
{
"PutRequest": {
"Item": {
"Column1": { "S": "Column1 Value" },
"Column2": { "S": "Column2 Value" },
"Column3": { "S": "Column3 Value" },
"Column4": { "S": "Column4 Value" },
}
}
}
]
}
You don't need to use the API. You could use the AWS-CLI instead, i.e:
aws dynamodb put-item --table-name MusicCollection --item file://item.json --return-consumed-capacity TOTAL
but you may need to tweak your JSON format a bit.
More examples and documentation here:
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/put-item.html
I have some JSON which has a variable number of fields. It will always have an "ID" field however.
What I want to know is: Does DynamoDB support just dumping this JSON in a table regardless of what fields it has? What I would like there to be would be an "ID" field and a "data" field in the table, where the "data" is simply all the JSON except the ID.
Without using Dynamo's specific structure I can't see how this is possible, but I wanted to get another opinion. Can Dynamo support JSON which has no set structure or schema whatsoever?
First approach
The benefit here is that you can store arbitrary data and should not care if DynamoDB supports it. You don't even need to care if this is a valid JSON. If you are storing DynamoDB List/Maps all attributes should be of types that DynamoDB supports.
You can push this approach even further and use compression and your item will occupy less space and save you some RCUs/WCUs in the process.
The first drawback is that it is harder to work with code like this. Simply because you need to convert data back and forth and this will make your code and operations more complicated.
The second drawback, DynamoDB won't know anything about your data and won't be able to access it. For example you will not be able to use document paths feature.
Second approach
I think this is a preferred approach unless you have a good reason to stick to the first one (e.g. unusual data or size constraints).
The benefit here is that it is easier to work with, you can access all DynamoDB features and if you are using DynamoDBMapper it is really easy to implement.
DynamoDB provides more query flexibility. First of all you can specify query expressions that will use fields in you maps/lists. Second you will be able to project attributes from DynamoDB data structures