1

I am looking to send JSON via Postman to the API Gateway API Endpoint, and am having difficulty with sending Map and SS attributes.

I have chosen Map for "Created", and "Last Modified" to show the date and time.

I have chosen SS for list items, e.g. "Ages": ["Grade 8","Grade 9","Grade 10"].

Please could someone help review the Lambda function, and JSON, to understand how to send data across? I am very new to AWS.

This will be a form for people to post jobs via a front-end.

Lambda Function

"use strict";
require("dotenv").config();
const AWS = require("aws-sdk");
AWS.config.update({ region: "eu-west-1" });
const documentClient = new AWS.DynamoDB.DocumentClient();

const addJobListing = async (data) => {
  const body = {
    Item: {
      PK: data["Item"]["PK"],
      SK: data["Item"]["SK"],
      "Days Off": data["Item"]["Days Off"],
      "Start Date": data["Item"]["Start Date"],
      "Job ID": data["Item"]["Job ID"],
      "Contract Length": data["Item"]["Contract Length"],
      "Expiration Date": data["Item"]["Expiration Date"],
      Created: {
        date: data["Item"]["Created Date"],
        time: data["Item"]["Created Time"],
      },
      Insurance: data["Item"]["Insurance"],
      Title: data["Item"]["Title"],
      Holidays: data["Item"]["Holidays"],
      Ages: data["Item"]["Ages"], //NEEDS TO BE MAPPED
      "Flight Reimbursement": data["Item"]["Flight Reimbursement"],
      Curriculum: data["Item"]["Curriculum"], //NEEDS TO BE MAPPED
      "Account ID": data["Item"]["Account ID"],
      "Minimum Monthly Salary Before Tax":
        data["Item"]["Minimum Monthly Salary Before Tax"],
      "Housing Allowance": data["Item"]["Housing Allowance"],
      Responsibilities: data["Item"]["Responsibilities"],
      "Job Closest Metro": data["Item"]["Job Closest Metro"],
      "Job Requirements": data["Item"]["Job Requirements"],
      "Job Type": data["Item"]["Job Type"],
      "Z-VISA Reimbursement": data["Item"]["Z-VISA Reimbursement"],
      Meals: data["Item"]["Meals"],
      Subject: data["Item"]["Subject"],
      "Salary Information": data["Item"]["Salary Information"],
      "Maximum Monthly Salary Before Tax":
        data["Item"]["Maximum Monthly Salary Before Tax"],
      "Vacancy Status": data["Item"]["Vacancy Status"],
      "Contract Completion Bonus": data["Item"]["Contract Completion Bonus"],
      "Last Modified": {
        date: data["Item"]["Modified Date"],
        time: data["Item"]["Modified Time"],
      },
    },
    TableName: "XXXX",
  };

  return await documentClient
    .put(body)
    .promise()
    .then((data) => console.log(data.Attributes))
    .catch(console.error);
};

exports.handler = async (event, context, callback) => {
  console.log("EVENT BODY", event.body);
  const response = await addJobListing(JSON.parse(event.body));
  return {
    statusCode: 200,
    headers: {
      "Access-Control-Allow-Origin": "*", // Required for CORS support to work
      "Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
    },
    body: JSON.stringify(response),
  };
};

JSON Test Via Postman:

{
    "Item": {
        "PK": "ACC#1000",
        "SK": "JOBID#01.17/01/2022",
        "Days Off": "Weekends",
      "Start Date": "17/09/2022",
      "Job ID": "01",
      "Contract Length": "Two Years",
      "Expiration Date": "17/09/2022",
      "Created": {
            "date": "17/09/2022",
            "time": "00:00:00"
      },
      "Insurance": "Insurance",
      "Title": "Title Of Vacancy",
      "Holidays": "Amount of Holidays",
      "Ages": ["Grade 8","Grade 9","Grade 10"],
      "Flight Reimbursement": "Flight Reimbursement",
      "Curriculum": ["Schools Own Curriculum","International Baccalaureate","GCSE"],
      "Account ID": "10",
      "Minimum Monthly Salary Before Tax": "Minimum Monthly Salary Before Tax",
      "Housing Allowance":"Housing Allowance",
      "Responsibilities": "Responsibilities",
      "Job Closest Metro": "Job Closest Metro",
      "Job Requirements": "Job Requirements",
      "Job Type": "Job Type",
      "Z-VISA Reimbursement": "Z-VISA Reimbursement",
      "Meals": "Meals",
      "Subject": "Subject",
      "Salary Information": "Salary Information",
      "Maximum Monthly Salary Before Tax": "Maximum Monthly Salary Before Tax",
      "Vacancy Status": "Vacancy Status",
      "Contract Completion Bonus": "Contract Completion Bonus",
      "Last Modified": {
            "date": "17/09/2022",
            "time": "00:00:00"
      }
      }
}
1
  • Can you add any error messages you're getting to your question? Generally, to be able to answer a question, it's best to put it in the form of "when I try X, I expect Y, but what I see is Z" Commented Jan 19, 2022 at 12:56

1 Answer 1

1

Initial Problem:

The issue was with the "Created"/"Last Modified" key/value pairs.

For Example this does not work, as it references "Created Date" and "Created Time" columns, which do not exist in the DynamoDB table:

  Created: {
    date: data["Item"]["Created Date"],
    time: data["Item"]["Created Time"],
  },

Solution:

Instead, "Created"/"Last Modified" should look like this:

  Created: data["Item"]["Created"],
  "Last Modified": data["Item"]["Last Modified"],

The "Created" can now be changed to a string too, or passed in the modified map attribute in JSON:

"Created": "2014-09-12T19:34:29Z",

Tip: When going on DynamoDB, one can add an item and click JSON to see how it would look as a JSON item.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.