3

I have array input like below in a intermediate AWS Step function state and I want to join that into a single string, I know States.StringSplit for splitting string with separator into array, does reverse of it possible with Intrinsic functions without writing a lambda function?

e.g: Input parameters as:

{
   "inputArray": ["a","b","c"],
   "joiner": "/"
}

I am expecting below ResultPath:

{"joinedValue": "a/b/c/"}

Or

{"joinedValue": "a/b/c"}
0

1 Answer 1

1

You can do it with a step function and no lambdas, but it isn't one step - you need a loop that combines all the pieces into one string:

      JoinArray:
        id: JoinArrayStateMachine
        name: ${self:service}-${self:provider.stage}-JoinArrayStateMachine
        type: EXPRESS
        definition:
          Comment: Join Array Orchestration
          Version: "1.0"
          StartAt: Make Input
          States:
            Make Input:
              Type: Pass
              Parameters:
                inputArray.$: States.StringSplit('a,b,c',',')
                joiner: /
              Next: Get First Item

            Get First Item:
              Type: Pass
              Parameters:
                index: 1
                increment: 1
                item.$: States.ArrayGetItem($.inputArray, 0)
                length.$: States.ArrayLength($.inputArray)
              ResultPath: $.iterator
              Next: Check Joined Items Length

            Check Joined Items Length:
              Type: Choice
              Choices:
                - Variable: $.joinedValue
                  IsPresent: false
                  Next: Create Joined Items
                - Variable: $.joinedValue
                  StringEquals: ''
                  Next: Create Joined Items
              Default: Append Joined Items

            Create Joined Items:
              Type: Pass
              Parameters:
                iterator.$: $.iterator
                inputArray.$: $.inputArray
                joiner.$: $.joiner
                joinedValue.$: $.iterator.item
              Next: Check Index

            Append Joined Items:
              Type: Pass
              Parameters:
                iterator.$: $.iterator
                inputArray.$: $.inputArray
                joiner.$: $.joiner
                joinedValue.$: States.Format('{}{}{}',$.joinedValue,$.joiner,$.iterator.item)
              Next: Check Index

            Check Index:
              Type: Choice
              Choices:
                - Variable: $.iterator.length
                  NumericGreaterThanPath: $.iterator.index
                  Next: Get Next Item
              Default: Finish

            Get Next Item:
              Type: Pass
              Parameters:
                index.$: States.MathAdd($.iterator.index, $.iterator.increment)
                increment.$: $.iterator.increment
                item.$: States.ArrayGetItem($.inputArray, $.iterator.index)
                length.$: States.ArrayLength($.inputArray)
              ResultPath: $.iterator
              Next: Check Joined Items Length

            Finish:
              Type: Pass
              Parameters:
                iterator: ()
                inputArray.$: $.inputArray
                joiner.$: $.joiner
                joinedValue.$: $.joinedValue
              Next: End

            End:
              Type: Succeed
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.