I decided to add a Part 8: Examples of using EVERY array function! - Part A
This is Part A where I setup a sample array data structure with several elements with each element containing a collection of key/value pairs. I apply just about every array function in different scenarios against this structure.
Here’s the JSON I parse in Parse JSON in module 1:
[
{
"name": "Alex C",
"age": 53,
"city": "Toronto"
},
{
"name": "Eric S",
"age": 43,
"city": "Hoboken"
},
{
"name": "Liana S"
},
{
"name": "David S"
}
]
Click this image to open and click again to zoom in:
Here are the expressions used in this Part A of the scenario walkthrough:
Get first array element: {{get(2.array; 1)}}
Get city of second array element: {{get(2.array; "2.city")}}
Get age from first array element using pick: {{pick(get(2.array; 1); "age")}}
Get all cities from each array element using map: {{map(2.array; "city")}}
Get unique city names: {{distinct(map(2.array; "city"))}}
Remove null values: {{remove(map(2.array; "city"); null)}}
This join won’t work. Pass primitive array without keys first!: {{join(2.array)}}
Create string of ages from array, remove empty ages: {{join(remove(map(2.array; "age"); null); "|")}}
Get length of array: {{length(2.array)}}
Get keys of array - must select element!: {{keys(last(2.array))}}
Slice out second collection from array: {{slice(2.array; 1; 2)}}
Merge 1st and 3rd array elements: {{merge(slice(2.array; 0; 1); slice(2.array; 2; 3))}}
Does array contain Hoboken?: {{contains(map(2.array; "city"); "Hoboken")}}
Create a new array with elements: {{add(emptyarray; "some text"; map(2.array; "name"); now)}}
Get names from array, filtered by age: {{map(2.array; "name"; "age"; 53)}}
Randomize name key from all elements of array: {{shuffle(map(2.array; "name"))}}
Randomize all elements of array: {{shuffle(2.array)}}
Sort array by age: {{sort(2.array; "age")}}
Reverse elements of array: {{reverse(2.array)}}
Convert collection to Array: {{toArray(get(2.array; 2))}}
Part B will contain a separate description on using flatten()
, deduplicate()
and toArray()
.
Copy and Paste this blueprint into a new Make Scenario
{
"subflows": [
{
"flow": [
{
"id": 1,
"module": "json:ParseJSON",
"version": 1,
"parameters": {
"type": ""
},
"mapper": {
"json": "[\n {\n \"name\": \"Alex C\",\n \"age\": 53,\n \"city\": \"Toronto\"\n },\n {\n \"name\": \"Eric S\",\n \"age\": 43,\n \"city\": \"Hoboken\"\n },\n {\n \"name\": \"Liana S\"\n },\n{\n \"name\": \"David S\"\n }\n]"
},
"metadata": {
"designer": {
"x": 0,
"y": 150
},
"restore": {
"parameters": {
"type": {
"label": "Choose a data structure"
}
}
},
"parameters": [
{
"name": "type",
"type": "udt",
"label": "Data structure"
}
],
"expect": [
{
"name": "json",
"type": "text",
"label": "JSON string",
"required": true
}
]
}
},
{
"id": 2,
"module": "builtin:BasicAggregator",
"version": 1,
"parameters": {
"feeder": 1
},
"mapper": {
"name": "{{1.name}}",
"age": "{{1.age}}",
"city": "{{1.city}}"
},
"metadata": {
"designer": {
"x": 300,
"y": 150
},
"restore": {
"extra": {
"feeder": {
"label": "JSON - Parse JSON [1]"
},
"target": {
"label": "Custom"
}
}
}
}
},
{
"id": 3,
"module": "util:SetVariables",
"version": 1,
"parameters": {},
"mapper": {
"variables": [
{
"name": "Get first array element",
"value": "{{get(2.array; 1)}}"
},
{
"name": "Get city of second array element",
"value": "{{get(2.array; \"2.city\")}}"
},
{
"name": "Get age from first array element using pick",
"value": "{{pick(get(2.array; 1); \"age\")}}"
},
{
"name": "Get all cities from each array element using map",
"value": "{{map(2.array; \"city\")}}"
},
{
"name": "Get unique city names",
"value": "{{distinct(map(2.array; \"city\"))}}"
},
{
"name": "Remove null values",
"value": "{{remove(map(2.array; \"city\"); null)}}"
},
{
"name": "This join won't work. Pass primitive array without keys first!",
"value": "{{join(2.array)}}"
},
{
"name": "Create string of ages from array, remove empty ages",
"value": "{{join(remove(map(2.array; \"age\"); null); \"|\")}}"
},
{
"name": "Get length of array",
"value": "{{length(2.array)}}"
},
{
"name": "Get keys of array - must select element!",
"value": "{{keys(last(2.array))}}"
},
{
"name": "Slice out second collection from array",
"value": "{{slice(2.array; 1; 2)}}"
},
{
"name": "Merge 1st and 3rd array elements",
"value": "{{merge(slice(2.array; 0; 1); slice(2.array; 2; 3))}}"
},
{
"name": "Does array contain Hoboken?",
"value": "{{contains(map(2.array; \"city\"); \"Hoboken\")}}"
},
{
"name": "Create a new array with elements",
"value": "{{add(emptyarray; \"some text\"; map(2.array; \"name\"); now)}}"
},
{
"name": "Get names from array, filtered by age",
"value": "{{map(2.array; \"name\"; \"age\"; 53)}}"
},
{
"name": "Randomize contents of array",
"value": "{{shuffle(2.array)}}"
},
{
"name": "Sort array by age",
"value": "{{sort(2.array; \"age\")}}"
},
{
"name": "Reverse array of names",
"value": "{{reverse(2.array)}}"
},
{
"name": "Convert collection to Array",
"value": "{{toArray(get(2.array; 2))}}"
}
],
"scope": "roundtrip"
},
"metadata": {
"designer": {
"x": 600,
"y": 150
},
"restore": {
"expect": {
"variables": {
"items": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
]
},
"scope": {
"label": "One cycle"
}
}
},
"expect": [
{
"name": "variables",
"type": "array",
"label": "Variables",
"spec": [
{
"name": "name",
"label": "Variable name",
"type": "text",
"required": true
},
{
"name": "value",
"label": "Variable value",
"type": "any"
}
]
},
{
"name": "scope",
"type": "select",
"label": "Variable lifetime",
"required": true,
"validate": {
"enum": [
"roundtrip",
"execution"
]
}
}
],
"interface": [
{
"name": "Get first array element",
"label": "Get first array element",
"type": "any"
},
{
"name": "Get city of second array element",
"label": "Get city of second array element",
"type": "any"
},
{
"name": "Get age from first array element using pick",
"label": "Get age from first array element using pick",
"type": "any"
},
{
"name": "Get all cities from each array element using map",
"label": "Get all cities from each array element using map",
"type": "any"
},
{
"name": "Get unique city names",
"label": "Get unique city names",
"type": "any"
},
{
"name": "Remove null values",
"label": "Remove null values",
"type": "any"
},
{
"name": "This join won't work. Pass primitive array without keys first!",
"label": "This join won't work. Pass primitive array without keys first!",
"type": "any"
},
{
"name": "Create string of ages from array, remove empty ages",
"label": "Create string of ages from array, remove empty ages",
"type": "any"
},
{
"name": "Get length of array",
"label": "Get length of array",
"type": "any"
},
{
"name": "Get keys of array - must select element!",
"label": "Get keys of array - must select element!",
"type": "any"
},
{
"name": "Slice out second collection from array",
"label": "Slice out second collection from array",
"type": "any"
},
{
"name": "Merge 1st and 3rd array elements",
"label": "Merge 1st and 3rd array elements",
"type": "any"
},
{
"name": "Does array contain Hoboken?",
"label": "Does array contain Hoboken?",
"type": "any"
},
{
"name": "Create a new array with elements",
"label": "Create a new array with elements",
"type": "any"
},
{
"name": "Get names from array, filtered by age",
"label": "Get names from array, filtered by age",
"type": "any"
},
{
"name": "Randomize contents of array",
"label": "Randomize contents of array",
"type": "any"
},
{
"name": "Sort array by age",
"label": "Sort array by age",
"type": "any"
},
{
"name": "Reverse array of names",
"label": "Reverse array of names",
"type": "any"
},
{
"name": "Convert collection to Array",
"label": "Convert collection to Array",
"type": "any"
}
]
}
}
]
}
],
"metadata": {
"version": 1
}
}
–
Alex Sirota
Director of NewPath Consulting - we are Make Heroes!
Check out my series of videos and scenario walkthroughs for Make Newbies