Access nested body property from HTTP resolver(AppSync)

529 Views Asked by At

I'm new to AWS AppSync and I am trying to access certain body property(from HTTP response) in my resolver's response mapping template. For example: I am able to present the response as is via $util.toJson($ctx.result.body), but when I try to get some of the nested body properties it fails.

For example, imagine the body looks like this:

{
  about:{
    "firstName":"Chuck",
    "lastName":"Norris"
  }
}

and $util.toJson($ctx.result.body.about) returns null. Any thoughts?

1

There are 1 best solutions below

0
On

I found a way extract the parsed body in the following way:

#set ($parsed_body = $util.parseJson($ctx.result.body))

And then I am able to access the properties via dot notation:

parsed_body.about.firstName

The part I was missing is $util.parseJson(<json-string>)

It seems that the body is a JSON string.