Toni Kuutti
Toni Kuutti

Reputation: 93

Appsync response mapping template json key name change

What would be the right way to change json response key value in aws appsync response mapping template?

JSON that I get looks like this:

{
  "tenant_id": 1,
  "id": "bd8ce6a8-8532-47ec-8b7f-dcd1f1603320",
  "header": "Header name",
  "visible": true
}

and what I would like to pass forward is

{
  "tenantId": 1,
  "id": "bd8ce6a8-8532-47ec-8b7f-dcd1f1603320",
  "header": "Header name",
  "visible": true
}

Schema wants tenant id in form of tenantID and lambda returns it in form of tenant_id. I could change it in lambda but I would like to know how to do it in response mapping template.

Upvotes: 7

Views: 4331

Answers (1)

Shankar Raju
Shankar Raju

Reputation: 4546

You could do this via the response mapping template for the field you are resolving to in the following manner:

Consider the JSON response from your lambda to be stored in the response variable, then you can return something like this.

$#set($result = {
 "tenantId": ${response.tenant_id},
 "id": "${response.id}",
 "header": "${response.header}",
 "visible": $response.visible
})

$util.toJson($result)

Alternatively, you could also mutate your response from the lambda by setting a tenantId field, something like #set( $response.tenantId = $response.tenant_id ). Let me know if you still face an issue.

Thanks, Shankar

Upvotes: 7

Related Questions