5

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?

3 Answers 3

11

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>();
Sign up to request clarification or add additional context in comments.

Comments

5

Also, do not forget to add/change aws serializer :

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AwsLambdaHandleTelegramWebhooks
{
   public class Function
   {
      public  APIGatewayProxyResponse FunctionHandler(JObject input,ILambdaContext context)
      {

Comments

0

What Solved My problem : I used [LambdaSerializer(typeof(Newtonsoft.Json.JsonSerializer))] on the Function Handler

and Assigned [JsonProperty("")] attribute to all POCO class Properties

and directly used the POCO class in the Handler Signature FunctionHandler(MyPocoClass input, ILambdaContext context)

The following is my Call in MAIN :

 private static async Task Main(string[] args)
    {
        Func<MyPocoClass , ILambdaContext, string> handler = FunctionHandler;
        await LambdaBootstrapBuilder.Create(handler, new DefaultLambdaJsonSerializer())
            .Build()
            .RunAsync();
    }

Following is My Handler Method:

[LambdaSerializer(typeof(Newtonsoft.Json.JsonSerializer))]
public static string FunctionHandler(MyPocoClass input, ILambdaContext context)
{
   // logic
}

MyPoco Class :

public class MyPocoClass
{
     [JsonProperty("MyProperty")]
     public string? MyProperty{ get; set; }
    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.