Search code examples
c#.netamazon-web-servicesaws-lambdaamazon-sqs

AWS Lambda (.NET) + SQS: Error converting the Lambda event JSON payload to a string


i use AWS Lambda (.NET Core 2.1 environment) + SQS as trigger

The problem is that my lambda cannot parse my SQS message.

Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example "Hello World" in order to be converted to a string: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.: JsonSerializerException

Here is declaration of my handler:

public async Task<string> FunctionHandler(DummyMessage message, ILambdaContext context)

Model:

public class DummyMessage {
  public string Label { get; set; }
}

SQS input from AWS Console that I tried: {"Label":"qwerty"}, "{"Label":"qwerty"}", "{\"Label\":\"qwerty\"}", but same error occurs.

Could you please help with this issue?


Solution

  • When passing Json try JObject from Newtonsoft.Json.Linq

    FunctionHandler(JObject eventStr, ILambdaContext context)
    

    and then you can Deseralize the response where SQSEvent is inherited from Amazon.Lambda.SQSEvents SDK Library.

    var result = eventStr.ToObject<SQSEvent>();