Step Functions - how to join an array?

0

As of now there is a States.StringSplit($.inputString, $.splitter) intrinsic function to split an string into an array. Is there a way to join an array into an string? Something like States.ArrayJoin($.inputString, $.joiner)? Is there any work around to achieve something like that?

Reason is that I want to replace a character in a string, since there is no replace I thought about splitting and joining which does have the same effect. I'm using an activity and building an url with the taskToken, but unfortunately any "+" character get's replace by a space, so I want to replace it with "%2B".

Jonas
已提問 9 個月前檢視次數 796 次
2 個答案
1

Yes, but not easily. You can iterate through the array of items you want to join using a Choice that flows to a Pass that uses States.Format to append each array item to a string. It's ugly, but it works.

  1. Create a Pass state that initializes the string accumulator and the number of items in the array, and flow to the Choice state below: Parameters: { "the_string": "", "item_count.$": "$.States.ArrayLength($.my_array)", "current_index": 0, "my_array.$": "$.my_array" }
  2. Create a choice state that tests the array length and current index to see if any items remain to be appended: Choices: [ { "Variable": "$.current_index", "NumericLessThanPath": "$.item_count", "Next": "Append an Item" } ]
  3. Create a Pass state that appends the string and increments the current index, and returns to the Choice state above: Parameters: { "the_string.$": "States.Format('{}your seprator here{}', $.the_string, $. )", "item_count.$": "$.item_count", "current_index.$": "States.MathAdd($.current_index, 1)", "my_array.$":"$.my_array" }

The Choice state will flow to the default Next state once all of the array items have been appended to $.the_string.

Hope that's clear enough.

mlhpdx
已回答 8 天前
0

One option will be to use States.Format() and pass the array contents if the array length is fixed.

AWS
已回答 9 個月前
  • Thanks, unfortunately the output of an taskToken does not have an constant number of "+". I already thought about a loop which combines always 2 array values as long as there are more than 1 value in the array. But for an Standard function that's also pretty expensive. Would be nice if there is a native function to achieve that.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南