Finally we arrive at Part 8: Examples of using EVERY Make array function!: Part B
In Part B, I illustrate the use of the flatten()
, deduplicate()
and the toCollection()
functions. These functions have their own special use and techniques.
We use JSON like this to illustrate flatten()
and deduplicate()
:
{
"nested-array": [
"item1",
"item2",
"item1",
"item2",
[
"itemsub1",
"itemsub2",
"itemsub1",
[
"itemsub3-sub1",
"itemsub3-sub2",
"itemsub3-sub2",
"itemsub3-sub3"
],
{
"itemsub3-coll1": "value1",
"itemsub3-coll2": "value2",
"itemsub3-coll3": "value1",
"itemsub3-coll4": "value3",
"itemsub3-coll5": "value4",
"itemsub3-coll6": "value3"
}
]
]
}
flatten()
serves to collapse nested arrays to an arbitrary depth level. For example, say we have an array like this:
[val1, val2, [val3, val4], val5]
Flattening this array to a depth level of 1 converts the array and, well, flattens it into
[val1, val2, val3, val4, val5]
Let’s add another level of nesting to our working array:
[val1, val2, [val3, [val4, val5]], val6]
Flattening this new array to a depth level of 2 converts the array to a totally flattened array:
[val1, val2, val3, val4, val5, val6]
But flattening the array to a depth level of 1 yields an array with val4 and val5 still nested 1 level (rather than nested 2 levels down):
[val1, val2, val3, [val4, val5], val6]
Using the expression {{flatten(9.
nested-array; 2)}}
we can flatten the nested-array
array in our JSON example to 2 levels. You should study this output carefully and compare to the original array to see how it has been collapsed. Note the collection remains untouched but it has been moved all the way up to the top level of the array.
"Flatten array 2 levels down": [
"item1",
"item2",
"item1",
"item2",
"itemsub1",
"itemsub2",
"itemsub1",
"itemsub3-sub1",
"itemsub3-sub2",
"itemsub3-sub2",
"itemsub3-sub3",
{
"itemsub3-coll1": "value1",
"itemsub3-coll2": "value2",
"itemsub3-coll3": "value1",
"itemsub3-coll4": "value3",
"itemsub3-coll5": "value4",
"itemsub3-coll6": "value3"
}
]
deduplicate()
serves to remove duplicate array values given a particular array. The deduplication returns the object with duplicates removed in the array specified. So if you called deduplicate(nested-array)
the top level 4 elements would be deduped leaving only 2 elements, item1 and item2. Note the remaining elements in the array remain untouched.
"Deduplicate array at level 1 ": [
"item1",
"item2",
[
"itemsub1",
"itemsub2",
"itemsub1",
[
"itemsub3-sub1",
"itemsub3-sub2",
"itemsub3-sub2",
"itemsub3-sub3"
],
{
"itemsub3-coll1": "value1",
"itemsub3-coll2": "value2",
"itemsub3-coll3": "value1",
"itemsub3-coll4": "value3",
"itemsub3-coll5": "value4",
"itemsub3-coll6": "value3"
}
]
]
You can of course call deduplicate()
with one of the nested arrays and the function will return the array at that level deduplicated. Calling {{deduplicate(get(9.
nested-array; 5))}}
will target the 5th element of nested-array
. The result is this array where itemsub1
as a duplicate value has been removed. Note that only the array and all its subitems are returned since we are working directly with the 5th element of the original nested-array
.
"Deduplicate array in an array ": [
"itemsub1",
"itemsub2",
[
"itemsub3-sub1",
"itemsub3-sub2",
"itemsub3-sub2",
"itemsub3-sub3"
],
{
"itemsub3-coll1": "value1",
"itemsub3-coll2": "value2",
"itemsub3-coll3": "value1",
"itemsub3-coll4": "value3",
"itemsub3-coll5": "value4",
"itemsub3-coll6": "value3"
}
]
To illustrate the toArray()
function, we can use this expression {{toArray(get(get(9.
nested-array; 5); 5))}}
which grabs the 5th element of the 5th element of the nested-array object – which is a collection. Note the nested get() functions are a longer way of expressing {{9.
nested-array.5.5}}
but they mean the same thing. This expression converts the collection to an array. Instead of 6 key value pairs in the original collection, we get an array of 6 elements with the keys key
and value
holding the original collection’s data.
"Convert collection into array": [
{
"key": "itemsub3-coll1",
"value": "value1"
},
{
"key": "itemsub3-coll2",
"value": "value2"
},
{
"key": "itemsub3-coll3",
"value": "value1"
},
{
"key": "itemsub3-coll4",
"value": "value3"
},
{
"key": "itemsub3-coll5",
"value": "value4"
},
{
"key": "itemsub3-coll6",
"value": "value3"
}
]
The usefulness of the toArray()
function comes into focus when we apply deduplicate()
and map()
on this array to get the unique values of the original collection using the expression {{deduplicate(map(toArray(9.
nested-array.5.5); "value"))}}
:
"Deduplicate values in a collection": [
"value1",
"value2",
"value3",
"value4"
]
To illustrate the toCollection()
function, we now switch to using this JSON array called email
:
{
"email": [
{
"fieldname": "Subject",
"value": "My subject is this!"
},
{
"fieldname": "To",
"value": "email@email.com"
},
{
"fieldname": "Content",
"value": "This is the email content."
}
]
}
Converting this array to a collection can be accomplished with the expression {{toCollection(11.email; "fieldname"; "value")}}
Note that the second argument of this call, fieldname
, is simply the name of the key whose value will be used to create the new key in the collection. The third argument, value
, is the simply the name of the key whose value will be used for the value of the new key in the collection. Study this output bundle to understand how an array gets converted to a collection. Note that if you iterated the original email
array you’d get 3 output bundles. Converting it to a collection yields 1 output bundle: a collection with 3 key/value pairs.
[
{
"Convert an array to a collection": {
"Subject": "My subject is this!",
"To": "email@email.com",
"Content": "This is the email content."
}
}
]
Download the Make blueprint and use the Import Blueprint to create a new scenario:
Part 8 Examples of using EVERY Make array function - part B.json (28.5 KB)
–
Alex Sirota
Director of NewPath Consulting - we are Make Heroes!
Check out my series of videos and scenario walkthroughs for Make Newbies