3

I am using a Lambda Function within Step Functions. The function is written in Java. I want to pass multiple values similiar as in this question.

The code of the function looks like so:

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;
}

The log in CloudWatch is as expected:

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

But when i go into the Step Function Console, i can see that the result was passed with escaped quotes:

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

This output can not be handled by the State Machine. I am not sure wether is this a only-java problem or also related to aws.

How can i pass the json-string with unescaped quotes, so it can be used by following components?

1 Answer 1

3

Instead of returning a String value, you can return an Object or something like a Map (using Map an example here)

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;
}

This way if you include (in the state definition)

 "ResultPath": "$.taskresult",

You will get this result in the output (along with other elements):

"taskresult": {
  "brs": "42",
  "rsm": "123"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.