Fix: JsonSerializerException in Lambda C#

An exception has occured in my Lamda function with C#.

    "errorType" : "JsonSerializerException",
    "errorMessage" : "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.",
    "stackTrace"   : [
        "at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream requestStream)",
        "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
    "cause"        : {
        "errorType" : "JsonReaderException",
        "errorMessage" : "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
        "stackTrace"   : [
            "at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
            "at Newtonsoft.Json.JsonTextReader.ReadAsString()",
            "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)",
            "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
            "at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)",
            "at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
            "at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream requestStream)"

My function is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace AWSLambda1
    public class Function
        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string FunctionHandler(string input, ILambdaContext context)
            var hello = "Hello, Lambda!!";


            return hello;

Input json is below:

  "key1": "value1",
  "key2": "value2",
  "key3": "value3"

JSON is valid but the exception occurs.

This cause is below:

Since your lambda function is expecting input to be a string, the framework tries to parse the input as though it's a string.

However, the input is actually a JSON object, not a string.

Therefore the parser will fail with an "unexpected character" error. The parser is expecting a " character which would indicate the start of a string.

Fix is very easy.

Change declaration of function.

public string FunctionHandler(string input, ILambdaContext context)


public string FunctionHandler(object input, ILambdaContext context)

That's fine in my environment.

  • Resource