How to pass an empty string as value of a field in dynamodb?

12,632

Solution 1

Yes Dynamodb cannot accepts empty string. So edit the aws configuration

var docClient = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true });

This works!

Solution 2

A map of attributes and their values. Each entry in this map consists of an attribute name and an attribute value. Attribute values must not be null; string and binary type attributes must have lengths greater than zero; and set type attributes must not be empty. Requests that contain empty values will be rejected with a ValidationException exception.

You can solve the problem by defining a function to remove empty string from the object like

function removeEmptyStringElements(obj) {
  for (var prop in obj) {
    if(obj[prop] === '') {// delete elements that are empty strings
      delete obj[prop];
    }
  }
  return obj;
}
removeEmptyStringElements(req.body);

This will remove the empty attributes from your object.

If your object contains nested object then use the following function

function removeEmptyStringElements(obj) {
  for (var prop in obj) {
    if (typeof obj[prop] === 'object') {// dive deeper in
      removeEmptyStringElements(obj[prop]);
    } else if(obj[prop] === '') {// delete elements that are empty strings
      delete obj[prop];
    }
  }

removeEmptyStringElements(req.body)

Solution 3

Empty values in non-key string/binary attributes is now supported - AWS announcement

Solution 4

Amazon DynamoDB now supports empty values for non-key String and Binary attributes in DynamoDB tables https://stackoverflow.com/a/61909830/7532347

Share:
12,632
liza
Author by

liza

Updated on July 10, 2022

Comments

  • liza
    liza almost 2 years

    I am trying to do a post request with the following json data. But i need one field ie 'notes' to pass as an empty string value. When I am passing like that, an error is getting :

    'One or more parameter values were invalid: An AttributeValue may not contain an empty string'.

    How can I fix this issue?

    //json data which i need to post
      {
      "storeId": "106",
      "addressId": "1",
      "managerId": "1",
      "name": "Syammohan",
      "contactNo": "9656985685",
      "notes": "",
      "bookingType": "Weddding Consult",
      "bookingDate": "2019-05-02",
      "bookingTime": "09:00 am"
    }
    
    function bookingDone(employee) {
      var {
    
        storeId,
        addressId,
        managerId,
        name,
        contactNo,
        notes,
        bookingType,
        bookingStatus,
        bookingTime
      } = req.body
    
      console.log("notes", notes);
    
      const params = {
        TableName: "Booking",
    
        Item: {
          id: id,
          storeId: storeId,
          addressId: addressId,
          managerId: managerId,
          name: name,
          contactNo: contactNo,
          notes: notes,
          bookingType: bookingType,
          bookingStatus: bookingStatus,
          bookingDate: bookingDate,
          bookingTime: bookingTime,
          employeeId: employee.id
    
        },
    
      };
    
      docClient.put(params, (error) => {
        if (error) {
          console.log(error);
          res.status(400).json({ error: 'Could not create booking' });
        }
        // queue.push(JSON.stringify({ event: 'booking.booking.created', model: { 'Bookings': params.Item } }));
        res.send(params.Item)
        // res.json({ id, name, info });
    
      });
    }
    
  • goldfrapp04
    goldfrapp04 almost 4 years
    Dynamo supports empty strings starting May 2020: aws.amazon.com/about-aws/whats-new/2020/05/…
  • João Pereira
    João Pereira almost 4 years
    Thanks for the link! Does the user need to update anything? I'm still getting the ValidationException error when I pass null to an optional
  • cellepo
    cellepo almost 4 years
    This repeats the identical update answer