AWS-Lambda – C# – JsonReaderException – Unexpected character encountered while parsing value (line 1, position 1)

This blog explains in more detail why you are probably getting the error and provides two solutions, depending on whether you want to pass just a string or JSON.

Error:

</pre>
{
"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 )"
]
}
</pre>

Solution – If you are passing JSON

I wanted to pass JSON, to at first I built my method like this:


public string HelloWorld(string input, ILambdaContext context) 

I then intended to deserialize the JSON into an object so I could reference each field. I had included my own Deserialize which was totally unneeded, as you will see in a moment:

MajesticData objMajesticData = JsonConvert.DeserializeObject<MajesticData>(jsonMajesticData);

I wanted to reference the fields in the following manner in order to pass parameters values to a SQL stored procedure.


myCommand.Parameters.AddWithValue("@domain", objMajesticData.domain);

My example data (this is what I passing to the Lambda test)

{
   "domain": "irvingseoexpert.com",
   "trustFlow": 18,
   "citationFlow": 25,
   "backlinks": 617,
   "error": null
}

My C# Data Class:


public struct MajesticData
{
   public string domain;
   public DateTime datetime;
   public int trustFlow;
   public int citationFlow;
   public int backlinks;
   public string error;
}

 

Well the answer turns out, that if you pass JSON, apparently they deserialize it for you. And you must specify the class, otherwise it looks like they try to deserialize it to the class “string”.So instead, I changed the method to use my class, and everything worked smoothly.


public string SaveMajesticData(MajesticData objMajesticData, ILambdaContext context)

Solution – If you are passing a string

Just pass the string in quotes.

Example, your method matches the HelloWorld example:


public string HelloWorld(string input, ILambdaContext context) 

I hope this explained better than I have seen explained in the AWS forums and on StackOverflow.com. The StackOverflow link mentions that you could use JObject instead of string, then pass {“yourVarname”,”your value”} (where “yourVarname” is the name of the parm and it is of type JObject.

Uncategorized  

Leave a Reply