java - Cannot get rid of '\' character in JSON once deployed by Lambda/Serverless java - Cannot get rid of '\' character in JSON once deployed by Lambda/Serverless json json

java - Cannot get rid of '\' character in JSON once deployed by Lambda/Serverless


Okay so I figured it out. Turns out serverless not only includes Jackson, but actually in the layout it creates for handling responses, the "setObjectBody" section will accept any kind of object and use Jackson to parse it to JSON. This is where I messed up. I assumed it would only accept Strings, which is where the double encoding was occurring. Now, if I pass in the Person object, serverless/Jackson handles it appropriately for me and the expected output is returned. I'll include code snippets below to better demonstrate this solution. Serverless creates a 'handler' class which has a template including a method called handleRequest. Once filled in, this class now looks like this:

public class GetStatusHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {private static final Logger LOG = Logger.getLogger(GetStatusHandler.class);@SuppressWarnings("unchecked")public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {    BasicConfigurator.configure();    LOG.info("received: " + input);      try {         Map<String, String> pathParameters = (Map<String, String>) input.get("queryStringParameters");         if(pathParameters == null) {             LOG.info("Getting details for all persons ");              PersonControl control = new PersonControl();              Person[] result = control.myGetHandler(context);               return ApiGatewayResponse.builder()                        .setStatusCode(200)                        .setObjectBody(result)                        .setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))                        .build();         }else {             String name = pathParameters.get("name");              LOG.info("Getting details for "+name);             PersonControl control = new PersonControl();             Person result = control.myGetHandler(name, context);             return ApiGatewayResponse.builder()                        .setStatusCode(200)                        .setObjectBody(result)                        .setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))                        .build();         }        }catch(Exception e) {            LOG.error(e, e);            Response responseBody = new Response("Failure getting person", null);            return ApiGatewayResponse.builder()                  .setStatusCode(500)                  .setObjectBody(responseBody)                  .setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))                  .build();        }    }}

Not that when returning the ApiGatewayResponse (via builder), an object is simply passed in to the .setObjectBody method ('result') which serverless automatically converts to JSON for us. Thats it! No parsing to JSON necessary in the code.


The response can be a user defined object as below

class Handler implements RequestHandler<SQSEvent, CustomObject> {    public CustomObject handleRequest(SQSEvent event, Context context) {        return new CustomObject();    }}

Sample code can be found here.


Just use the Google Gson java library that can be used to convert Java Objects into their JSON representation.

Gson gson = new Gson();gson.toJson(person);