Boto3: Empty Attribute would cause ValidationException for DynamoDb

Created on 24 Mar 2017  路  7Comments  路  Source: boto/boto3

aws-sdk-js now support option to convert empty value to null for DynamoDb.

It would be nice if it is supported for python.

Converting empty values to null is now an opt-in feature of the document client; you can pass a boolean convertEmptyValues option to the document client constructor to have it do so. I'll open a PR to improve the documentation of this feature.

https://github.com/aws/aws-sdk-js/issues/833

feature-request

Most helpful comment

Any chance this feature is going to get prioritized in 2020?

All 7 comments

Marking this as a feature request.

+1

I'm wrestling with this right now myself. Using boto3 and lambda via Serverless framework.

Just bumped into this, any news?

FWIW, just ran into that snag. This little recursion solved the issue for our data schema:

def convert_empty_values(d):
    for k, v in d.iteritems():
        if isinstance(v, dict):
            convert_empty_values(v)
        elif isinstance(v, list):
            for i in v:
                convert_empty_values(i)
        elif v == "":
            d[k] = None
    return d

Any chance this feature is going to get prioritized in 2020?

You can try this:

def dict_to_dynamo(raw):
    if isinstance(raw, dict):
        for k, v in raw.items():
            raw[k] = dict_to_dynamo(v)
    elif isinstance(raw, list):
        for i in range(len(raw)):
            raw[i] = dict_to_dynamo(raw[i])
    elif raw == "":
        raw = None

    return raw

FWIW, just ran into that snag. This little recursion solved the issue for our data schema:

def convert_empty_values(d):
    for k, v in d.iteritems():
        if isinstance(v, dict):
            convert_empty_values(v)
        elif isinstance(v, list):
            for i in v:
                convert_empty_values(i)
        elif v == "":
            d[k] = None
    return d

This is awesome, thanks! Iteritems is out of date though, and in python3 dictionaries have a .items method and lists don't . So I had to rewrite this for python3:

def convert_empty_values(dictOrList):  #Use this function wrapped around a dyanmo create/update dict in order to make sure there are no empty strings
    if isinstance(dictOrList, dict):
        for key, value in dictOrList.items():
            if isinstance(value, dict) | isinstance(value, list):
                convert_empty_values(value)
            elif value == "":
                dictOrList[key] = None
    elif isinstance(dictOrList, list):
        for value in dictOrList:
            if isinstance(value, dict) | isinstance(value, list):
                convert_empty_values(value)
            elif value == "":
                dictOrList.remove(value)
                dictOrList.append(None)
    return dictOrList
Was this page helpful?
0 / 5 - 0 ratings