1

I'm trying to send JSON data with an array to an AWS Lambda function, but I can't parse the array.

In the frontend I'm requesting this way:

const ids = ['long_id1', 'long_id2', 'long_id3'];

const request = await fetch(
  'https://ID.execute-api.us-east-1.amazonaws.com/default/endpoint',
  {
    headers: {
      "accept": "application/json, text/plain, */*",
      "content-type": "application/json",
    },
    body: JSON.stringify({
      userIds: ids
    }),
    method: 'POST'
 }
)
const response = JSON.parse(await request.text());

In the lambda I have the following code:

exports.handler = async (event) => {
  const parsed = JSON.parse(event); //also tried: JSON.parse(JSON.stringify(event)) because I saw someone recommending it

  return {
    statusCode: 200,
    body: JSON.stringify(parsed)
  }

}

But I get event.body as a string. For example if I log the event variable I get:

event: {
  ...,
  body: "{\"userIds\":[\"long_id1\",\"long_id2\",\"long_id3\"]}"
}

But, if I try to parse the body, it throws this error:

ERROR   SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at Runtime.exports.handler (/var/task/index.js:11:23)
    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)
2021-08-08T07:43:05.425Z 5feeddb7-6254-405d-b311-42924abc5f4d ERROR SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at Runtime.exports.handler (/var/task/index.js:11:23) at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)

Any idea what am I doing wrong?

Update

Not sure if was some kind of typo but now I'm able to get the data properly. I'm just doing what @Mark B said with the following code in the lambda function:

exports.handler = async (event) => {
  const { userIds } = JSON.parse(event.body);

  const result = await doSomethingWithUserIds(userIds)

  return {
    statusCode: 200,
    body: JSON.stringify(result)
  }

}

Thanks for all the help :)

8
  • 2
    Why are you taking the event object, converting it to JSON, converting it back from JSON, then converting it to JSON again? Commented Aug 8, 2021 at 8:02
  • 2
    "it throws an error" — What error do you get? Commented Aug 8, 2021 at 8:02
  • "I try to parse it," — Which of the multiple times you parse something in your code are you talking about here? Commented Aug 8, 2021 at 8:03
  • hi @Quentin, thanks for showing me how I can improve my question. I just updated here the error. I tried converting it to JSON and then it back from JSON because I saw someone recommending it. But I get the same error regardless Commented Aug 8, 2021 at 8:12
  • 1
    event is an object. You don't need to parse that into JSON, it's already a JSON object. event.body is a string. You need to parse that property of the event object into JSON. JSON.parse(event.body). Commented Aug 8, 2021 at 14:40

2 Answers 2

1

event is an object. You don't need to parse that into JSON, it's already a JSON object. event.body is a string. You need to parse that property of the event object into JSON.

JSON.parse(event.body)
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if was some kind of typo but now I'm able to get the data properly. I'm just doing what @Mark B said with the following code in the lambda function:

exports.handler = async (event) => {
  const { userIds } = JSON.parse(event.body);

  const result = await doSomethingWithUserIds(userIds)

  return {
    statusCode: 200,
    body: JSON.stringify(result)
  }

}

Thanks for all the help :)

2 Comments

A better option would be to ask @MarkB to write up an answer so that you can accept it.
Thanks @jarmod, I didn't think this before. I'm going to ask him :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.