Skip to content

retrieve element of JSON string from api gateway integration response stepfunction payload

0

I"m using the REST api gateway to call a sync stepfunction. The response payload (below) has an attribute "output" that contains the result of the last step execution.

{
  "billingDetails": {
    "billedDurationInMilliseconds": 100,
    "billedMemoryUsedInMB": 64
  },
  "executionArn": "arn:aws:states:us-east-1:REDACT:express:getGithubConnectionStepFunction:a0a03625-6b07-45e1-b008-6131e9f1ad55:8a2d74a2-ac46-46d5-9170-b48634a59858",
  "input": "{\"CustomerId\":\"3d82e1a7-b749-469d-b53c-31d4a223c730\"}",
  "inputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "name": "a0a03625-6b07-45e1-b008-6131e9f1ad55",
  "output": "{\"statusCode\":200,\"body\":[{\"CustomerId\":\"3d82e1a7-b749-469d-b53c-31d4a223c730\",\"Name\":\"githubtoken\",\"CreationTime\":\"2024-12-13T18:49:28Z\",\"LastModifiedTime\":\"2024-12-13T18:49:28Z\"},{\"CustomerId\":\"3d82e1a7-b749-469d-b53c-31d4a223c730\",\"Name\":\"githubtoken2\",\"CreationTime\":\"2024-12-16T00:42:35Z\",\"LastModifiedTime\":\"2024-12-16T00:42:35Z\"}]}",
  "outputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "startDate": 1734352762.293,
  "stateMachineArn": "arn:aws:states:us-east-1:REDACT:stateMachine:getGithubConnectionStepFunction",
  "status": "SUCCEEDED",
  "stopDate": 1734352762.383,
  "traceHeader": "Root=1-67601f7a-4d3d83228df4554dcb02ccfb;Sampled=1"
}

I can't seem to extract the body from this payload. If i try to use $util.parseJson($input.path('$.output')) I get this weird non-json payload

{statusCode=200, body=[{CustomerId=3d82e1a7-b749-469d-b53c-31d4a223c730, Name=githubtoken, CreationTime=2024-12-13T18:49:28Z, LastModifiedTime=2024-12-13T18:49:28Z}, {CustomerId=3d82e1a7-b749-469d-b53c-31d4a223c730, Name=githubtoken2, CreationTime=2024-12-16T00:42:35Z, LastModifiedTime=2024-12-16T00:42:35Z}]}

When i try to simply access the body member that I want I get no result. #set ($blahObj = $input.path('$.output')) $blahObj.body

NOTHING

I'm not sure what else to try.

2 Answers
0
  1. First, parse the entire response to get access to the 'output' field: #set($stepFunctionResponse = $util.parseJson($input.body))

  2. Then, parse the 'output' field, which is itself a JSON string: #set($outputJson = $util.parseJson($stepFunctionResponse.output))

  3. Now you can access the body: #set($body = $outputJson.body)

  4. If you want to return just the body in your API response, you can do: $util.toJson($body)

This approach first parses the entire Step Functions response, then specifically parses the 'output' field which contains your desired data. After these steps, you should be able to access the body content correctly.

If you want to modify the response structure or include additional fields, you can create a new object and populate it with the data you need:

#set($response = {}) $response.put("statusCode", $outputJson.statusCode) $response.put("body", $body)

$util.toJson($response)

This will give you a properly formatted JSON response with the status code and body from the Step Functions output.

Remember to adjust your API Gateway method response and integration response settings to ensure the correct content type (application/json) is set and that the response is passed through correctly.

Sources
Set up an integration response in API Gateway - Amazon API Gateway
Create API Gateway REST APIs with Step Functions - AWS Step Functions

answered 2 years ago

0

I think the issue is with the input.path. Try input.json, like this: $util.parseJson($input.json('$.output')).

AWS
EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.