Whenever I call a post API to my dynamodb, all the data gets store in one "content" attribute. Is there a way to have a separate attribute for each attribute in the JSON structure?

For instance here is where I am calling the API, the JSON structure of data is in the values variable.

export default (async function submitSite(values) {
  console.log(values);
  createSite(values);
})

function createSite(site) {
  return API.post("sites", "/sites", {
    body: site
  });
}

and here is the code for the API itself:

export async function main(event, context) {
  const data = JSON.parse(event.body);

  console.log(data);
  var site = "IE"+data.siteCounty+data.siteName;
  var siteIdStripped = site.replace(/[aeiou]/g, '');
  var siteId = siteIdStripped.replace(/ /g,'');
  var siteIdFinal = siteId.toUpperCase();

  const params = {
    TableName: "sites",
    Item: {
      userId: event.requestContext.identity.cognitoIdentityId,
      siteId: siteIdFinal,
      content: data,
      createdAt: Date.now()
    }
  };

When I upload it is has the structure siteId, userId, content(with an object of data), created at.

Id like for it to be displayed as siteId, userId, siteName, siteAddress, etc etc the last 2 being one of the indexes in the json

1 Answers

1
Mark B On Best Solutions

You are explicitly creating the content attribute here: content: data,. You either need to explicitly set each attribute like this:

Item: {
  userId: event.requestContext.identity.cognitoIdentityId,
  siteId: siteIdFinal,
  siteName: data.siteName,
  siteAddress: data.siteAddress,
  createdAt: Date.now()
}

Or, you could simplify this with the object spread operator like so:

Item: {
  ...data,
  userId: event.requestContext.identity.cognitoIdentityId,
  siteId: siteIdFinal,
  createdAt: Date.now()
}