Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

Sometimes your handler is trying to get the wrong kind of input object. Here's an error I got:

Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

What this meant in my case was that I was trying to pass a JSON object from API Gateway to my Lambda function and I used the example Handler.java code in the AWS documentation. However, the object I was getting sent was not just a Map of String keys with String values. Some of the values were actually arrays or maps themselves.

In this case I simply changed the class definition from this:

public class Handler implements RequestHandler<Map<String, String>, String> {

To this:

public class Handler implements RequestHandler<Map<String, Object>, String> { 

And now I get a Map<String, Object> that contains all of my nested objects. It's better to build your own custom object to avoid having to dig into the map manually but this works for testing.

Don't forget to change your handler from this:

@Override
public String handleRequest(Map<String, String> event, Context context) {

To this:

@Override
public String handleRequest(Map<String, Object> event, Context context) {