Data structures

Restructuring data with loops 

As we start to see different data types flow through our events, we'll pick up a new common trend. A lot of things are in arrays 🤯. These arrays are lists that include items within.

When working with array* data in Tines, we often need to restructure or filter that data to get only the specific elements we want.

*Arrays: explained 

💡Note

Array example: 

In the example below, we have two different objects that have arrays. The people object includes items with name, place, and URL. The second object we have is IPs, that only contain IP addresses in the array.

We can always identify arrays in Tines with the [] brackets.

{
  "people": [
    {
      "name": "Alice",
      "place": "New York",
      "url": "https://www.example1.com"
    },
    {
      "name": "Bob",
      "place": "Los Angeles",
      "url": "https://www.example2.com"
    },
    {
      "name": "Carol",
      "place": "London",
      "url": "https://www.example3.com"
    }
  ],
  "IPs": [
    "192.168.1.1",
    "192.168.1.2",
    "192.168.1.3",
    "192.168.1.4",
    "192.168.1.5",
    "192.168.1.6",
    "192.168.1.7",
    "192.168.1.8",
    "192.168.1.9",
    "192.168.1.10"
  ]
}

A common way to work with arrays is by leveraging the implode and explode functions. We will get a basic understanding before reviewing a video on how to apply this technique.

1) Using explode to get what we need from an array

The explode action can break apart an array into individual elements. This allows you to work with each element separately.

2) Using event transform to modify the data

a) Setting up a new data structure

Within an event transform action, you can use a loop through each exploded array element. Here you can define the new data structure for that individual element. This could be trimming your array to just be the main things you need, or it could be reformatting it to look however you need.

b) Applying logic in between as needed with formulas

Within this loop, you can use formulas to filter or modify the individual array elements as needed before defining the new data structure. This can be great to do some data validation and correcting some of the info. Maybe if a field is blank before you want it to display as "Unknown" or something else.

3) Using Implode to pull individual items back together

Once you've defined the new data structure within the loop, you can use the implode action to combine the individual elements back into an array with the new structure.

▲  How-to: leverage explode and implode
Was this helpful?