Why not able to access JSON properties from lambda function

0

My lamda function handler looks like this

export const handler = async (event, context, callback) => {

   context.callbackWaitsForEmptyEventLoop = false;

   var data = event.body;

var id=data.companyid;//gets undefined
var orders = data.orders;//gets undefined

   let response = {
      statusCode: 200,
      headers: {          
      },
      body: ""
  };
  console.log("response: " + JSON.stringify(response))  
    callback(null, response);
};

The request is a post with json data.

I am not able to access the properties in the json object. When I try to get value of "companyid"or "orders", I get undefined.

my json request (short simplified version):

{"companyid":"31434","orders":["452455","486783","418784"]}

Why am I not able to access "companyid" or "orders".

additional notes:

1.Initially I tried var data = JSON.parse(event.body) . But that does not work : gets "unexpected token in JSON at position" error. There is no problem with the request json. This is a working nodejs app that I am trying to convert to lambda.

  1. console.log(event) shows the below result
{
  version: '2.0',
  routeKey: 'ANY /stringspush',
  rawPath: '/default/stringspush',
  rawQueryString: '',
  headers: {
    accept: '*/*',
    'accept-encoding': 'gzip, deflate, br',
    'cache-control': 'no-cache',
    'content-length': '295686',
    'content-type': 'application/json',
    host: '7x8hl42b61.execute-api.us-west-1.amazonaws.com',
    'postman-token': '2c029ae3-6911-41af-b643-771d6273123a',
    'user-agent': 'PostmanRuntime/7.28.4',
    'x-amzn-trace-id': 'Root=1-61e58198-762e4002234732094ad50029',
    'x-forwarded-for': '122.171.25.249',
    'x-forwarded-port': '443',
    'x-forwarded-proto': 'https'
  },
  requestContext: {
    accountId: '811697223032',
    apiId: '7x8hl42b61',
    domainName: '7x8hl42b61.execute-api.us-west-1.amazonaws.com',
    domainPrefix: '7x8hl42b61',
    http: {
      method: 'POST',
      path: '/default/stringspush',
      protocol: 'HTTP/1.1',
      sourceIp: '122.171.25.249',
      userAgent: 'PostmanRuntime/7.28.4'
    },
    requestId: 'MGEwBj74yK4EJqA=',
    routeKey: 'ANY /stringspush',
    stage: 'default',
    time: '17/Jan/2022:14:47:53 +0000',
    timeEpoch: 1642430873544
  },
  body: `{"companyid":"5287","strings":["5452245", "7564534"]}`,
  isBase64Encoded: false
}
  1. console.log(event.body)
{"companyid":"5287","strings":["5452245", "7564534"]}
az-gi
asked 2 years ago2011 views
2 Answers
0

What is the actual output when you do console.log(JSON.stringify(event)); Maybe this gives a clue.

ET
answered 2 years ago
  • console.log output added to question

0

From the log it seems the body is a string. You need to convert to JSON in order to access the attributes. The strange thing is that it shows with ``` and not with ' like the rest of items in there. Not sure why and if it makes a difference. Maybe this is why JSON.parse fails.

profile pictureAWS
EXPERT
Uri
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions