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

Related searches

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 });

  });
}

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)

Empty attribute values cannot be added to a DynamoDB table , 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� DynamoDB does not support empty sets, however, empty string and binary values are allowed within a set. The following example shows a string set, a number set, and a binary set: ["Black", "Green", "Red"] [42.2, -19, 7.5, 3.14] ["U3Vubnk=", "UmFpbnk=", "U25vd3k="]

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

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

This works!

Amazon DynamoDB now supports empty values for non-key String , List, Map, and Set data types also support empty String and Binary values. Partition key and sort key attributes of base tables continue to require� In AWS SDK for Java 1.0, DynamoDBMapper supports reading of empty String attribute values, however, it will not write empty String attribute values because these attributes are dropped from the request. DynamoDB supports the Java Set, List, and Map collection types. The following table summarizes how these Java types map to the DynamoDB types.

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

An AttributeValue may not contain an empty string, The mapper should know that if DynamoDb doesn't have a value for that specific attribute for a given key, to set it as null. Also, the mapper should� This lead to the inability to retrieve all the values from DynamoDB on my GET requests due to parsing errors originating from the values DynamoDB was populating the optional fields with. So I resolved to provide a default value whenever a field value is not supplied when saving data to DynamoDB.

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

Why doesn't DynamoDB allow empty strings?, Attribute values cannot be null. String and Binary type attributes must have lengths greater than zero. Set type attributes cannot be empty. You see, when I give give this library an object which contains properties that have empty string values, that's not an invalid object. That is a perfectly valid object. The fact that dynamoDb doesn't accept empty string doesn't make my object "invalid". So marshaller shouldn't be throwing exceptions when it finds such object.

it might help to think about why dynamoDB does not silently convert an Empty string into null. Maybe its better to let the application decide what to do if an empty string appears. Ideally if you want to convert an Empty string into null before writing it to DynamoDB, I would do it as: Coerce(my data); // do stuff such as covert empty string to null

Google Cloud Bigtable will let you store empty strings as values. I'm not quite as sure about whether you can successfully set a 0-byte value and distinguish it from an empty string ['\0'] value; the answer may depend on the client library/language/API that you use to interact with the service. [Source/Disclaimer: I work on Google Cloud Bigtable.]

This behavior seems to make DynamoDB a little more challenging to use - anywhere you could have an empty string, you’re going to have to write special case code to either remove that attribute before writing or map that empty string to a dummy value (like a Null data type or “NO_VALUE” or equivalent). References:

Comments
  • dynamo doesn't accept empty string, why not put some generic string or leave the field altogether?
  • Are you aware of that DDB is noSql?
  • Your problem is not NodeJS related. Can't you set "notes": null?
  • From docs Attribute values cannot be null. String and Binary type attributes must have lengths greater than zero.
  • As of June 2020, DynamoDB now supports empty strings natively. Make sure to turn off convertEmptyValues or else empty strings will be converted to NULL. stackoverflow.com/a/62166912/1457398
  • Dynamo supports empty strings starting May 2020: aws.amazon.com/about-aws/whats-new/2020/05/…
  • 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
  • This repeats the identical update answer