[array help] Pick the collection item with the lowest value & return a given value

Fairly straightforward goal: Return the value of the level (array key) for the lowest value in the key of the array item cost_excl_tax

(ive included copy/paste of a json module which outputs the data as displayed here - see bottom of post, you can copy the JSON and paste into any scenario)

Example structure of a collection/array:

In this example which returns 5 collections the lowest value of the key cost_excl_tax is the 5th item in the collection with the value 5.69… so I’d want to “return” the value of the level - in this case “MAIL”.

I feel like I had a good handle of get/map functions but honestly I can never seem to fully grasp how to do this correctly… I hope Make creates an AI co pilot for creation of these complex functions someday hah.

Thoughts? Much appreciated!

The full array (example)

[
            {
                "id": 29,
                "carrier_service_name": "FedEx 2 Day",
                "level": "EXPEDITED",
                "postbox_ok": false,
                "home_only": false,
                "business_only": false,
                "traceable": true,
                "transit_time": 2,
                "cost_excl_tax": 13.69,
                "currency": "USD",
                "is_active": true,
                "total_days_min": 7,
                "total_days_max": 9,
                "min_dispatch_date": "2024-02-09",
                "max_dispatch_date": "2024-02-13",
                "min_delivery_date": "2024-02-13",
                "max_delivery_date": "2024-02-15"
            },
            {
                "id": 31,
                "carrier_service_name": "FedEx Home",
                "level": "GROUND_HD",
                "postbox_ok": false,
                "home_only": true,
                "business_only": false,
                "traceable": true,
                "transit_time": 5,
                "cost_excl_tax": 11.69,
                "currency": "USD",
                "is_active": true,
                "total_days_min": 10,
                "total_days_max": 12,
                "min_dispatch_date": "2024-02-09",
                "max_dispatch_date": "2024-02-13",
                "min_delivery_date": "2024-02-16",
                "max_delivery_date": "2024-02-20"
            },
            {
                "id": 32,
                "carrier_service_name": "FedEx Standard Overnight",
                "level": "EXPRESS",
                "postbox_ok": false,
                "home_only": false,
                "business_only": false,
                "traceable": true,
                "transit_time": 1,
                "cost_excl_tax": 23.69,
                "currency": "USD",
                "is_active": true,
                "total_days_min": 6,
                "total_days_max": 8,
                "min_dispatch_date": "2024-02-09",
                "max_dispatch_date": "2024-02-13",
                "min_delivery_date": "2024-02-12",
                "max_delivery_date": "2024-02-14"
            },
            {
                "id": 34,
                "carrier_service_name": "USPS Priority Mail",
                "level": "PRIORITY_MAIL",
                "postbox_ok": true,
                "home_only": false,
                "business_only": false,
                "traceable": true,
                "transit_time": 5,
                "cost_excl_tax": 11.94,
                "currency": "USD",
                "is_active": true,
                "total_days_min": 10,
                "total_days_max": 12,
                "min_dispatch_date": "2024-02-09",
                "max_dispatch_date": "2024-02-13",
                "min_delivery_date": "2024-02-16",
                "max_delivery_date": "2024-02-20"
            },
            {
                "id": 1241,
                "carrier_service_name": "UPS Mail Innovation",
                "level": "MAIL",
                "postbox_ok": true,
                "home_only": false,
                "business_only": false,
                "traceable": true,
                "transit_time": 7,
                "cost_excl_tax": 5.69,
                "currency": "USD",
                "is_active": true,
                "total_days_min": 12,
                "total_days_max": 14,
                "min_dispatch_date": "2024-02-09",
                "max_dispatch_date": "2024-02-13",
                "min_delivery_date": "2024-02-20",
                "max_delivery_date": "2024-02-22"
            }
        ]

For Testing:

Copy/Paste JSON module which can be used for testing
{
    "subflows": [
        {
            "flow": [
                {
                    "id": 16,
                    "module": "json:ParseJSON",
                    "version": 1,
                    "parameters": {
                        "type": ""
                    },
                    "mapper": {
                        "json": "[\n  {\n    \"data\": [\n      {\n        \"id\": 29,\n        \"carrier_service_name\": \"FedEx 2 Day\",\n        \"level\": \"EXPEDITED\",\n        \"postbox_ok\": false,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 2,\n        \"cost_excl_tax\": 13.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 7,\n        \"total_days_max\": 9,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-13\",\n        \"max_delivery_date\": \"2024-02-15\"\n      },\n      {\n        \"id\": 31,\n        \"carrier_service_name\": \"FedEx Home\",\n        \"level\": \"GROUND_HD\",\n        \"postbox_ok\": false,\n        \"home_only\": true,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 5,\n        \"cost_excl_tax\": 11.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 10,\n        \"total_days_max\": 12,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-16\",\n        \"max_delivery_date\": \"2024-02-20\"\n      },\n      {\n        \"id\": 32,\n        \"carrier_service_name\": \"FedEx Standard Overnight\",\n        \"level\": \"EXPRESS\",\n        \"postbox_ok\": false,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 1,\n        \"cost_excl_tax\": 23.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 6,\n        \"total_days_max\": 8,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-12\",\n        \"max_delivery_date\": \"2024-02-14\"\n      },\n      {\n        \"id\": 34,\n        \"carrier_service_name\": \"USPS Priority Mail\",\n        \"level\": \"PRIORITY_MAIL\",\n        \"postbox_ok\": true,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 5,\n        \"cost_excl_tax\": 11.94,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 10,\n        \"total_days_max\": 12,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-16\",\n        \"max_delivery_date\": \"2024-02-20\"\n      },\n      {\n        \"id\": 1241,\n        \"carrier_service_name\": \"UPS Mail Innovation\",\n        \"level\": \"MAIL\",\n        \"postbox_ok\": true,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 7,\n        \"cost_excl_tax\": 5.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 12,\n        \"total_days_max\": 14,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-20\",\n        \"max_delivery_date\": \"2024-02-22\"\n      }\n    ]\n  }\n]"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1984,
                            "y": 465,
                            "name": "example structure",
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "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
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

Ok, I think I found a single module solution, the best kind hah. (figured I’d post for folks looking for similar solution in the future…). There may be a simpler way, but this works.


^downloadable at bottom of this reply

  1. We have to MAP the values of the key: cost_excl_tax - this returns all the values in a primitive array. {{map(17.data; "cost_excl_tax")}}
  2. Next, we have to sort the array values smallest to largest with the sort() functionality. This sorts the array: {{sort(map(17.data; "cost_excl_tax"); "asc")}}
  3. We then need to return the value for the first item (the smallest item). So we use the get() functionality with a “1” which returns the very first value {{get(sort(map(17.data; "cost_excl_tax"); "asc"); 1)}}. In this case, “5.69”
  4. Next we need to use the map() function to return the “level” value based on our specific “5.69” value… so we use the following function that builds on the previous functions…

map( [data array] ; level <-the key which value i want ; cost_excl_tax ← the key to search by ; 5.69 ← the value to search for.

remember our 5.69 was found via our original get/sort/map function… so we just have to put them all together into:

{{map(17.data; "level"; "cost_excl_tax"; get(sort(map(17.data; "cost_excl_tax"); "asc"); 1))}}

… yea it’s kinda silly, but it works.

Here’s the Modules JSON for anyone looking to emulate:

copy/paste into a scenario
{
    "subflows": [
        {
            "flow": [
                {
                    "id": 17,
                    "module": "json:ParseJSON",
                    "version": 1,
                    "parameters": {
                        "type": ""
                    },
                    "mapper": {
                        "json": "[\n  {\n    \"data\": [\n      {\n        \"id\": 29,\n        \"carrier_service_name\": \"FedEx 2 Day\",\n        \"level\": \"EXPEDITED\",\n        \"postbox_ok\": false,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 2,\n        \"cost_excl_tax\": 13.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 7,\n        \"total_days_max\": 9,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-13\",\n        \"max_delivery_date\": \"2024-02-15\"\n      },\n      {\n        \"id\": 31,\n        \"carrier_service_name\": \"FedEx Home\",\n        \"level\": \"GROUND_HD\",\n        \"postbox_ok\": false,\n        \"home_only\": true,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 5,\n        \"cost_excl_tax\": 11.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 10,\n        \"total_days_max\": 12,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-16\",\n        \"max_delivery_date\": \"2024-02-20\"\n      },\n      {\n        \"id\": 32,\n        \"carrier_service_name\": \"FedEx Standard Overnight\",\n        \"level\": \"EXPRESS\",\n        \"postbox_ok\": false,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 1,\n        \"cost_excl_tax\": 23.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 6,\n        \"total_days_max\": 8,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-12\",\n        \"max_delivery_date\": \"2024-02-14\"\n      },\n      {\n        \"id\": 34,\n        \"carrier_service_name\": \"USPS Priority Mail\",\n        \"level\": \"PRIORITY_MAIL\",\n        \"postbox_ok\": true,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 5,\n        \"cost_excl_tax\": 11.94,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 10,\n        \"total_days_max\": 12,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-16\",\n        \"max_delivery_date\": \"2024-02-20\"\n      },\n      {\n        \"id\": 1241,\n        \"carrier_service_name\": \"UPS Mail Innovation\",\n        \"level\": \"MAIL\",\n        \"postbox_ok\": true,\n        \"home_only\": false,\n        \"business_only\": false,\n        \"traceable\": true,\n        \"transit_time\": 7,\n        \"cost_excl_tax\": 5.69,\n        \"currency\": \"USD\",\n        \"is_active\": true,\n        \"total_days_min\": 12,\n        \"total_days_max\": 14,\n        \"min_dispatch_date\": \"2024-02-09\",\n        \"max_dispatch_date\": \"2024-02-13\",\n        \"min_delivery_date\": \"2024-02-20\",\n        \"max_delivery_date\": \"2024-02-22\"\n      }\n    ]\n  }\n]"
                    },
                    "metadata": {
                        "designer": {
                            "x": 703,
                            "y": 836,
                            "name": "example structure"
                        },
                        "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": 18,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "variables": [
                            {
                                "name": "get lowest value",
                                "value": "{{get(sort(map(17.data; \"cost_excl_tax\"); \"asc\"); 1)}}"
                            },
                            {
                                "name": "get the specific shipping level of lowest",
                                "value": "{{map(17.data; \"level\"; \"cost_excl_tax\"; get(sort(map(17.data; \"cost_excl_tax\"); \"asc\"); 1))}}"
                            }
                        ],
                        "scope": "roundtrip"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1034,
                            "y": 848
                        },
                        "restore": {
                            "expect": {
                                "variables": {
                                    "items": [
                                        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 lowest value",
                                "label": "get lowest value",
                                "type": "any"
                            },
                            {
                                "name": "get the specific shipping level of lowest",
                                "label": "get the specific shipping level of lowest",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
2 Likes

It’s not silly at all! A terrific demonstration of using nested functions to manipulate arrays. There is a min() function that takes an array of numbers as an argument so maybe you could use that instead of the get(sort(map( combo you put together.

3 Likes

Ahh Alex, that’s a good catch! that is indeed a much simpler approach for the first set of steps…

both return the same value… thanks!

2 Likes

Hello @Sean_Vosler :blob_wave:

Wow! I am honestly so impressed by the fact you were able to crack this by yourself. It is truly great to see Makers improve and get more proficient at using Make.

Thanks a lot for keeping the community in mind and coming back here with additional information and solution.

Great job and keep it up!

1 Like