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
feita há 9 meses796 visualizações
2 Respostas
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
respondido há 8 dias
0

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

AWS
respondido há 9 meses
  • 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.

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas