AWS BotoCore Error - An AttributeValue may not contain an empty string

an attributevalue may not contain an empty string dynamodb
one or more parameter values were invalid: an attributevalue may not contain an empty string
dynamodb empty string workaround
boto3 dynamodb
dynamodb insert null value
dynamodb empty list
expressionattributevalues must not be empty
convert empty values dynamodb

I'm trying to fill a DynamoDB database with data from an old event store made out of a PostgreSQL database. After it ran through a good portion of the db entries, this error was thrown when attempting to call the put_item function.

botocore.exceptions.ClientError:-

An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: An AttributeValue may not contain an empty string

I decided to rerun the code and see what was happening by dumping out all of the table attributes right before it was inserted.

I can see the only "empty string" is in the answer_string attribute of a dictionary, called details, see below:-

Importing event type 5 completed by user: 1933
1933 5 {'answer': {'difficulty': 1, 'answer_string': ''}, 'card_id': n 
'13448', 'review_id': '153339', 'time_spent': 2431}
62 153339
2017-01-18 00:46:48.373009+00:00 2017-01-18 00:46:48.364217+00:00

I'm pretty certain this is what's causing the error to be thrown, as none of the other table attributes are incorrect.

My problem is the details dictionary can come from dozens of different locations and each details dictionary can have different attributes - the one with the answer_string attribute is just one of many possible dictionary configurations. I can't possibly check for all possible configurations of a dictionary and verify that they all don't have empty strings.

Is there a way I can do a one time overall check on a dictionary and see if any one part of it is empty?

If you want to get a dictionary just containing all keys with empty values, you can simply apply a dictionary comprehension to the details-dict to get all key-value pairs with empty values. E.g.:

empty_values = {key: value for key, value in details.items() if not value}

If you instead want to filter out the key-value pairs with empty values, so you're left with a dictionary where all keys have values, simply use the same comprehension without the not:

details = {key: value for key, value in details.items() if value}

An AttributeValue may not contain an, AttributeValues and attribute names may not contain empty strings, AWS Error Message: Supplied AttributeValue is empty, must contain� If you want to get a dictionary just containing all keys with empty values, you can simply apply a dictionary comprehension to the details-dict to get all key-value pairs with empty values. E.g.: empty_values = {key: value for key, value in details.items() if not value}

Or, if you want replace all empty strings with None values:

def removeEmptyString(dic):
    for e in dic:
        if isinstance(dic[e], dict):
            dic[e] = removeEmptyString(dic[e])
        if (isinstance(dic[e], str) and dic[e] == ""):
            dic[e] = None
        if isinstance(dic[e], list):
            for entry in dic[e]:
                removeEmptyString(entry)
    return dic

dictionaryWithEmptyStringsReplacedWithNone = removeEmptyString(dicrionaryWithEmptyStrings)

It is far from perfect but it works.

DynamoDB.DocumentClient should support empty string properties , When the object contains a property with an empty string, it results in an exception instead of [AWS dynamodb 400 0.588s 0 retries] putItem({ TableName: 'my-test-table', Item: Error was { "message": "One or more parameter values were invalid: An AttributeValue may not contain an empty string ", "code":� The AWS Persistence Framework for Core Data should remove the attribute from the row when an existing attribute is set to nil or an empty string. Do you have a code snippet for reproducing the "One or more parameter values were invalid: An AttributeValue may not contain an empty string."

@PedoDorf's function worked for me though I had to add a check since sometimes it'd return "TypeError: string indices must be integers" when receiving a string

def removeEmptyString(dic):
  if isinstance(dic, str):
    if dic == "":
      return None
    else:
      return dic

  for e in dic:
    if isinstance(dic[e], dict):
      dic[e] = removeEmptyString(dic[e])
    if (isinstance(dic[e], str) and dic[e] == ""):
      dic[e] = None
    if isinstance(dic[e], list):
      for entry in dic[e]:
        removeEmptyString(entry)
  return dic

Thanks

An AttributeValue may not contain an empty string for key � Issue #87 , If my struct property might be empty, I have to take dynamodb. An AttributeValue may not contain an empty string for key #87 github.com/aws/aws -sdk-go-v2/service/dynamodb/expression is a godsend! in the far bottom left hand corner of the screen to send this UI bug to the DynamoDB console team. This is a service side limitation, attribute values cannot be created with an empty string value (not sure the exact reasoning for this). What is the expected behavior in the SDK? Copy link Quote reply

If you need to account for nested objects, and clean them as well, give this a try. Requires some recursion:

def clean_ddb_data(obj):
    cleaned = {}
    for k, v in obj.items():
        if isinstance(v, dict):
            cleaned[k] = clean_ddb_data(v)
        elif isinstance(v, str):
            if len(v) > 0:
                cleaned[k]=v
        else:
            cleaned[k]=v
    return cleaned

python, I want to write the following problem data to AWS DynamoDB. There are about 100 question data, but since it does not nec. One or more parameter values were invalid: An AttributeValue may not contain an empty string. String and Binary type attributes must have lengths greater than zero. Set type attributes cannot be empty. Requests with empty values will be rejected with a ValidationException exception. This could be something we support in the future by having something like an ignoreEmptyStrings configuration option for the DocumentClient that was opt in.

Common DynamoDB Errors (and Solutions), This page is a compilation of the most common DynamoDB user/system errors and parameter values were invalid: An AttributeValue may not contain an empty string � Error aka you did something wrong, and 5xx aka problem is on AWS side. Boto3 Queries � DynamoDB with AWS SAM � DynamoDB Capacity Modes� Empty String and Binary attribute values are allowed. Attribute values of type String and Binary must have a length greater than zero if the attribute is used as a key attribute for a table or index. Set type attributes cannot be empty. Invalid Requests with empty values will be rejected with a ValidationException exception.

DynamoDB — Boto3 Docs 1.14.32 documentation, For more information, see Batch Operations and Error Handling in the Amazon Each attribute value is described as a name-value pair. Attribute values must not be null; string and binary type attributes must have lengths greater than zero; � It doesn’t allow empty sets either. This seems to have been a source of much consternation (see references below) since day #1 and continues to be. The documentation states: Attribute values cannot be null. String and Binary type attributes must have lengths greater than zero. Set type attributes cannot be empty.

aws/aws-sdk-go, So easy to do with botocore with Python but it appears to be difficult with Go as you In addition the SDK does contain some helper functions which might help in this case. Problem is I'm currently not able to decrypt the password data. values were invalid: An AttributeValue may not contain an empty string" What's the� Type: String to AttributeValue object map Required: No. FilterExpression. A string that contains conditions that DynamoDB applies after the Query operation, but before the data is returned to you. Items that do not satisfy the FilterExpression criteria are not returned.

Comments
  • Thanks for the advice - unfortunately this only works for the outermost key value pairs. It is possible that a key's value is another dictionary, and that dictionary can have empty values as keys. Is there a way to extend this dictionary comprehension to cover a deeper scope?
  • If course, e.g. check out the following question and its answers: stackoverflow.com/questions/10756427/…
  • Thank you very much - I'll probably have to write my own helper function to get this done then. I'll give this a shot