2017-06-21 62 views
0

我正在使用API​​網關並具有將數據傳遞到Step Function的服務。是否有可能將所有階段變量添加到嵌套JSON結構中的正文映射模板中?

步驟功能需要在以下格式JSON輸入:

{ 
    "input":"", 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
} 

我目前通過階段變量以手動方式即

#set($data = $util.escapeJavaScript($input.json('$'))) 
{ 
    "input": "{ 
     \"input\": $data, 
     \"stageVariables\" : { 
      \"clientId\": \"${stageVariables.clientId}\", 
      \"clientSecret\": \"${stageVariables.clientSecret}\", 
      \"password\": \"${stageVariables.password}\" }, 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
} 

我知道你可以使用默認映射模板像這樣循環通過舞臺變量並生成一個JSON:

"stage-variables" : { 
#foreach($key in $stageVariables.keySet()) 
    "$key" : "$util.escapeJavaScript($stageVariables.get($key))" 
    #if($foreach.hasNext),#end 
#end 
} 

但是,步驟功能所需的JSON稍有不同。如何將所有階段變量轉換爲我的JSON格式,而無需手動顯式添加每個變量?

謝謝

回答

0

我認爲這應該工作,但我沒有測試它。我只是複製了你在那裏的foreach塊,並且在行內逃脫了引號。

#set($data = $util.escapeJavaScript($input.json('$'))) 
{ 
    "input": "{ 
     \"input\": $data, 
     \"stageVariables\" : { 
      #foreach($key in $stageVariables.keySet()) 
      \"$key\" : \"$util.escapeJavaScript($stageVariables.get($key))\" 
      #if($foreach.hasNext),#end 
      #end 
     }, 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
} 
+0

感謝您的回覆@JackKohn,請參閱我發佈的問題的答案。親切的問候。 – cyorkston

0

下面是工作的解決方案。看起來該模板對換行非常敏感,請注意下面的代碼總共包含5行。

#set($data = $util.escapeJavaScript($input.json('$'))) 
{ 
    "input": "{ \"input\": $data, \"stageVariables\" : { #foreach($key in $stageVariables.keySet()) \"$key\" : \"$util.escapeJavaScript($stageVariables.get($key))\" #if($foreach.hasNext),#end #end } }", 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
} 
相關問題