Customizing JSON response in HTTP APIs

I’m currently using the new HTTP API flavor of API Gateway (as opposed to the typical REST API flavor) to build an API. differences described here

I’d like to customize the JSON in my API response, since my database (DynamoDB) is not giving me exactly the format I’d like to see in the client. However, it’s not clear to me how to do this.

One of the cool features of API Gateway is it’s ability to transform the API response to any format using VTL. However, that feature is only available in the REST API flavor of API Gateway, not the newer and much cheaper HTTP API version. Since I do not have the power of API Gateway to transform the response, what’s the idomatic way to customize the JSON response in my API?

I find it hard to believe that I have to manually massage the response from DynamoDB in order to return the JSON I want. There has to be a better way to do this!

Perhaps it’s best to give an example, borrowed from Alex Debries article on one-to-many modeling in DynamoDB. Imagine I have a DynamoDB table that looks like this:

When I do a query for all rows in the Microsoft Organization, I get back JSON that looks like this:

 "OrgName": "Microsoft",
 "PlanType": "Enterprise"
  "Username":"Bill Gates",
  "UserType": "Member"
  }, (and so on....)

and I wan’t my API to return the following JSON to the client:

{ "org":
    { "name": "microsoft",
      "planType": "Enterprise",
          {"username":"Bill Gates",
          {"username":"Satya Nadella",
           (and so on....)

Most API examples I see online assume the format that comes out of DynamoDB is exactly what the API returns. However, as this example shows, I want to return a different format.

How are people doing this in their own APIs?

Am I correct to assume you’re using lambda proxy integration? You may want to use a projection to tailor your db response or simply manipulate the response json before stringifying it onto the lambda response body.

I’m new to DynamoDB, so I’m learning the vocabulary as I go. I was not aware of Projection Expressions, thanks for that tip!

It appears that Projection Expressions allow me to filter the results of a query, which will definitely be useful. However, I’m not sure it’s quite what I’m looking for in terms of shaping the JSON response from my API.

I’m finding that your second suggestions is likely what I’ll be doing (e.g. manipulate the response JSON before stringifying). I had wondered if people were using any particular technique/tools to manipulate the response. I suppose manipulating maps in Javascript isn’t too awful :slight_smile: