Invalid type for AWS DynamoDB put-item via CLI (unicode v. dict)
Solution 1
you need to add the type information (I assume String here)
aws dynamodb put-item --table-name my_table --item '{ "id": {"S": "1" } }'
Solution 2
We cannot provide a plain JSON schema as an input to the dynamodb put-item
command. We need to provide the attribute(s) data type along with its value.
But if you have a complex JSON schema and you are new to the aws dynamodb cli
commands then it becomes really tedious for you to prepare your input JSON file.
But we have an easy solution as well, please follow these steps:
- Prepare a sample JSON schema which you want to insert into your dynamodb table.
- Login to AWS Console, go to DynamoDB -> Tables -> , and from the top tabs chose
Items
, it will show you the data if any already. - Now click on the
Create Item
, it will open a new window and from there choose the optionText
instead ofTree
. Paste your sample JSON schema there and save this item. - Now Select that record and from
Actions
choose the optionExport to .csv
. This csv file contains your JSON attribute name (keys), their corresponding values along with their data type. - Now most of the work has been done for you and you can easily construct the input JSON file for your
dynamodb put-item
command.
Related videos on Youtube
samcorcos
React, React-Native, GraphQL, Phoenix/Elixir, Lambda, Serverless...
Updated on July 09, 2022Comments
-
samcorcos almost 2 years
I would like to add an item to my DynamoDB table via command line, but I've run into a type error.
The data that I'm trying to add is very simple:
{ "id": "1" }
The command I'm running is equally simple:
aws dynamodb put-item --table-name my_table --item '{ "id": "1" }'
The error I'm getting is:
Invalid type for parameter Item.id, value: 1, type: <type 'unicode'>, valid types: <type 'dict'>
I come from a JavaScript background, so I'm not familiar with
dict
types. From what I understand from some of the sources I've read, this is a Python thing? How do I change my data into something that DynamoDB can handle? -
samcorcos over 7 yearsAhh, I see what you mean. That's an interesting decision on their part. I guess the next step is to write a script to automate type discovery and reformatting. Thanks! docs.aws.amazon.com/cli/latest/reference/dynamodb/…
-
Sahas over 5 yearsThis is great answer..not sure if its documented anywhere but after adding the type information, it works :)