Day #6 with Cloud Workflows: arrays and dictionaries


So far, in this series of articles on Cloud Workflows, we have used simple data types, like strings, numbers and boolean values. However, it’s possible to use more complex data structures, like arrays and dictionaries. In this new episode, we’re going to use those new structures.


Arrays can be defined inline (like anArray) or spanning over several lines (like anotherArray):


- assignment:
    assign:
        - anArray: ["a", "b", "c"]
        - anotherArray:
            - one
            - two
- output:
    return: ${anArray[0] + anotherArray[1]}


The output step will return the string “atwo”.


For dictionaries, you can define them as follows:


- assignment:
    assign:
        - person:
            firstname: "Guillaume"
            lastname: "Laforge"
            age: 43
            kids: ["Marion", "Erine"]
- output:
    return: ${person.firstname + " and " + person.kids[1]}


The output step will return the string “Guillaume and Erine”. 


Notice that we nested an array within a dictionary. So you can easily create dictionaries containing arrays, containing other dictionaries, etc, just like any JSON or YAML structures. 


In the example we were able to access the second kid of the person, mixing both the field (dot) and index (square brackets) notations to access fields of our dictionary, and elements of our array.


This video shows both arrays and dictionaries in action:



In the coming articles, we’ll see that such data structures are handy for dealing with API endpoint calls.


 

 
© 2012 Guillaume Laforge | The views and opinions expressed here are mine and don't reflect the ones from my employer.