在 Step Function 中将 Json 字符串传递给 AWS Lambda - JsonReaderException 错误

Aji*_*oel 3 c# .net-core aws-lambda aws-step-functions

我正在尝试在 Step Function 中使用 AWS Lambda 函数。Lambda 函数在单独测试并转义 json 输入时正常工作。但是,当输入通过 step 函数传递给 lambda 函数时,我收到了 JsonReaderException 错误。我究竟做错了什么?社区会知道解决此问题的方法吗?

拉姆达函数:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Formatters.Binary;
using Amazon.Lambda.Core;
using Newtonsoft.Json.Linq;

// 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
    {
        public void PostsBasedOnOddOrEven(string input, ILambdaContext context)
        {
            var details = JObject.Parse(input);
            var postId = (int) details["id"];
            var oddOrEvenResult = (int) details["OddOrEvenPostsResult"];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从 Step Function 输入到 Lambda 函数:

{
  "id": "1",
  "OddOrEvenPostsResult": 2
}
Run Code Online (Sandbox Code Playgroud)

Lambda 函数的输入(通过 Visual Studio Invoke 工作):

"{ \"id\": \"1\", \"OddOrEvenPostsResult\": 2}"
Run Code Online (Sandbox Code Playgroud)

异常堆栈跟踪:

{
  "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)",
    "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
  ]
}
Run Code Online (Sandbox Code Playgroud)

当 Lambda 函数是 Step Function 的一部分时它不起作用

当 Lambda 函数是 Step Function 的一部分时它不起作用

Lambda 函数在单独测试时工作

Lambda 函数在单独测试时工作

Nic*_*zer 7

由于您的 lambda 函数预期input为 a string,因此框架会尝试将输入解析为string.

然而,输入实际上是一个 JSON 对象,而不是一个字符串。

因此,解析器将因“意外字符”错误而失败。解析器需要一个"表示字符串开始的字符。


因此,您可以通过以下方式修复它:

  1. 声明表示输入的 ac# 类

    public class FunctionInput
    {
        public int id { get; set; }
        public int OddOrEvenPostsResult { get; set; }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将您的函数签名更改为期望input的类型FunctionInput

    public class Function
    {    
        public void PostsBasedOnOddOrEven(FunctionInput input, ILambdaContext context)
        {
            var postId = input.id;
            var oddOrEvenResult = input.OddOrEvenPostsResult;
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

注意:您不需要自己解析输入。