Amazon Lambda in C# - JsonReaderException

Amazon Lambda in C# - JsonReaderException

aws lambda c# json input
aws api gateway c# example
aws lambda logging c#
aws lambda async c#
aws lambda environment variables c#
invoke aws lambda function c#
aws lambda exception handling c#
aws lambda dynamodb c# example

I am attempting to write an AWS Lambda function in C#. I have the AWS Toolkit for Visual Studio 2015. I created a project with the AWS Lambda Project (.Net Core) and then selected the Empty Function option. Which gave me the following code:

UPDATE & ANSWER 02/24/17 - The comment marked as the Answer was useful knowledge but was not the actual answer for me. It was @PavelSafronov's comment in that answer that did the trick. I was either passing nothing in (and getting the error) or I assumed it wanted ME to give the information in JSON format so I would enter { "input": "Some string" } and still get the error. Now that I just passed in "Some string" it worked. However, as far as I am concerned, this seems like a bug. A string is by default nullable and the code written by Amazon even assumes it could be by virtual of the input?.ToUpper() where the ?. is checking for null.

Note: I added the LambdaLogger.Log lines and the Constructor to see where I was getting:

using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.Json;

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

namespace AWSLambdaTest1 {

  public class Function {

    public Function() {
      LambdaLogger.Log("Within the Constructor");
    }

    public string KevinsTestFunction(string input, ILambdaContext context) {
      LambdaLogger.Log("Within the KTF");
      return input?.ToUpper();
    }

  }
}

The output screen and Solution Explorer were saying:

Errors in C:\Test Projects\AWSLambda1\AWSLambda1\AWSLambda1.xproj
  Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0'.

However, this will build and publish to AWS without fail. I can even Invoke it which returns the following - And is my main question:

{
  "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 lambda_method(Closure , Stream , Stream , ContextInfo )"
  ]
}

The Log file showed that the Constructors log message got there but NOT the actual KevingsTestFunction log message.

On a side note, I am able to get the Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0' error to go away by adding the following to my project.json file:

"runtimes": {
  "win10-x64": {},
  "win81-x64": {},
  "win8-x64": {},
  "win7-x64": {}
}

Which makes sense on a Windows machine, not so much here on Amazon. Do I need some different runtime(s)?

That change did not change the JsonReaderException exception.

I tried adding "Linux": {} but that made no change either.

I even tried to update the NuGet package Microsoft.NETCore.App from 1.0.0 to 1.1.0 and that did nothing as well and even back to 1.0.1.

I would figure the default example they give you would work, but I am wrong there. It seems like there are issues with the Amazon.Lambda.Serialization.Json.JsonSerializer attribute. Is it possible to just use NewtonSoft?


My experience with AWS Lambda in C# in a little limited, but this is the kind of error I get when the input ("test" data if you are triggering the function from the console) is not in a actual JSON format. The best bet might be to create a dummy input class for your testing purposes and then give the lambda function the equivalent JSON data to get it to serialize properly.

As for your project.json, I have not needed to add the runtimes section for AWS Lambda before. If you do then you will need to select the relevant runtimes from this list: https://docs.microsoft.com/en-us/dotnet/articles/core/rid-catalog

Also here is a example project.json file if you find that useful: https://gist.github.com/TAGray/8cc812065c3b6abcd732b7f6a3bda92a

Hope that helps.

Trying the C# Default Lambda - Getting, Trying the C# Default Lambda - Getting JsonReaderException. Posted by: I am attempting to write an AWS Lambda function in C#. I have the  blank-csharp – A C# function that shows the use of Lambda's .NET libraries, logging, environment variables, AWS X-Ray tracing, unit tests, and the AWS SDK. ec2-spot – A function that manages spot instance requests in Amazon EC2.


Per Pavel Safronov -

Yes you should be entering a string not a json object if your Lambda Function entry point is:

public string KevinsTestFunction(string input, ILambdaContext context) {

Then the string you could enter is:

"{ \"input\": \"Something\" }"

You could also change the Lambda Functions entry point to:

public string KevinsTestFunction(JObject input, ILambdaContext context) {

Then you can enter a json object like so:

{ "input": "Something" }

If you're not expecting any input and merely polling a SQS queue or DynamoDb table then you could change the Lambda Function's entry point to:

public string KevinsTestFunction(ILambdaContext context) {

Many variations to play around with.


Now per the update in the original question, by default the AWS web console TEST area will set you up with default test data to send to your Lambda Function. That default test data is a JSON object. As noted it is at odds with the default templates for the C# Lambdas (which accept the string input) made available thru Visual Studio via the AWS SDK. Perhaps this is a built in stumbling block (feature) to force us to run into this issue, pull our hair out over it, then realize how versatile the function handler can be...

Sample Lambda Hello World function not, Norm@AWS helped with a problem getting my Lambda function uploaded. In my case, I use AWS API Gateway to make Lambda functions available, so my lambda c# function is like JsonReaderException at Newtonsoft. 2 thoughts on “ AWS-Lambda – C# – JsonReaderException – Unexpected character encountered while parsing value (line 1, position 1) ” Sundar says: September 24, 2018 at 7:13 pm


Update the code:

public string KevinsTestFunction(string input, ILambdaContext context) {
  LambdaLogger.Log("Within the KTF");
  return input?.ToUpper();
}

to:

public string KevinsTestFunction(IDictionary input, ILambdaContext context) {
  LambdaLogger.Log("Within the KTF");
  return input["input"]?.ToUpper();
}

Do not forget to

using System.Collections;

Pass Json string to AWS Lambda in Step, Formatters.Binary; using Amazon.Lambda.Core; using Newtonsoft.Json string to AWS Lambda in Step Function - JsonReaderException error. To migrate existing AWS Lambda functions running in .NET Core 2.1 (C#/PowerShell) to .NET Core 3.1, update your code to be compatible with .NET Core 3.1 and Amazon Linux 2, and then update the function runtime to .NET Core 3.1.


AWS-Lambda - C# - JsonReaderException, AWS-Lambda – C# – JsonReaderException – Unexpected character encountered while parsing value (line 1, position 1). October 27, 2017. This blog explains  Lambda is looking for is a JSON string "string including quotes" not a JSON object {"prop":"value"} You should change the input for your test to be a JSON string or change the function to take an object. The object option is a little more involved. You need to add a NuGet reference to. Amazon.Lambda.Serialization.Json and add


Amazon Lambda in C# - JsonReaderException - c#, Amazon Lambda in C# - JsonReaderException. I am attempting to write an AWS Lambda function in C#. I have the AWS Toolkit for Visual Studio 2015. I created  Amazon.Lambda.Serialization.Json. This package contains a custom Amazon.Lambda.Core.ILambdaSerializer implementation which uses Newtonsoft.Json 9.0.1 to serialize/deserialize .NET types in Lambda functions. This serializer can be present on the assembly or on the handler method. If you specify both, the method attribute takes priority.


aws/aws-lambda-dotnet, Created an "Empty Function" new solution with VS2017 using AWS Toolkit JsonReaderException: Unexpected character encountered while parsing value: {​. I have already tried the C# object approach to bind the connect  AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. You can use AWS Lambda to extend other AWS services with custom logic, or create your own back-end services that operate at AWS scale, performance, and security.