使用 java 在 aws Step Functions 中传递 Json 作为结果路径而不转义引号

Passing Json as result path in aws Step Functions using java without escaping quotes

我在 Step Functions 中使用 Lambda 函数。函数写在Java中。我想传递多个类似于 .

的值

函数代码如下所示:

public String handleRequest(S3Event event, Context context) {

//other code...

String eventJsonString = event.toJson();
JsonParser parser = new JsonParser();
JsonObject eventJson = parser.parse(eventJsonString).getAsJsonObject();
eventJson.addProperty("value1", value1);
eventJson.addProperty("value2", value2);

String output = eventJson.toString().replace("\", "");
logger.log("output: " + output);
return output;
}

CloudWatch 中的日志符合预期:

output:
{
    "Records": [
        {
            "awsRegion": "eu-west-1",
            "eventName": "ObjectCreated:Put",
            "eventSource": "aws:s3",
            "eventTime": "1970-01-01T00:00:00.000Z",
            "eventVersion": "2.0",
        .
        .
        .
}

但是当我进入 Step Function 控制台时,我可以看到结果是通过转义引号传递的:

"{\"Records\":[{\"awsRegion\":\"eu-west-1\",\"eventName\":\"ObjectCreated:Put\",\"eventSource\":\"aws:s3\",\"eventTime\":\"1970-01-01T00:00:00.000Z\",\"eventVersion\":\"2.0\",...}

状态机无法处理此输出。 我不确定这是唯一的-java问题还是与aws有关。

我如何传递带有未转义引号的 json 字符串,以便它可以被以下组件使用?

您可以 return 一个对象或类似 Map 的对象(此处使用 Map 作为示例),而不是 return 字符串值

public Map<String, String> handleRequest(Map<String, Object> input, Context context) {

    String brs = "42";
    String rsm = "123";

    Map<String, String> output = new HashMap<>();
    output.put("brs", brs);
    output.put("rsm", rsm);

    return output;
}

如果你包括(在状态定义中)这样

 "ResultPath": "$.taskresult",

您将在输出中获得此结果(以及其他元素):

"taskresult": {
  "brs": "42",
  "rsm": "123"
}