Help Creating Test Script Loop to Update Variables

I have JSON response that contains the “id” field under “items” multiple times. Each one represents a different subinterface. How can I capture each “id” value and store in a separate variable? For instance, “id”: “0050568A-7152-0ed3-0000-261993067431” represents “ifname”: “V_MGMT” and “id”: “0050568A-7152-0ed3-0000-261993067432” represents “ifname”: “V_WiFi_MGMT”. My goal is to store each ID value in a variable for each interface, like this.
V_MGMTid=0050568A-7152-0ed3-0000-261993067431
V_WiFi_MGMTid=0050568A-7152-0ed3-0000-261993067432

{
    "links": {
        "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces?bulk=true"
    },
    "items": [
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067431"
            },
            "type": "SubInterface",
            "vlanId": 590,
            "subIntfId": 590,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "372cd4dc-a344-11ed-9afa-eb0671f93dfa",
                "type": "SecurityZone"
            },
            "ifname": "V_MGMT",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.161",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_MGMT 590",
            "id": "0050568A-7152-0ed3-0000-261993067431",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067432"
            },
            "type": "SubInterface",
            "vlanId": 591,
            "subIntfId": 591,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "4dc72e72-a344-11ed-9afa-eb0671f93dfa",
                "type": "SecurityZone"
            },
            "ifname": "V_WiFi_MGMT",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.193",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_WiFi_MGMT 591",
            "id": "0050568A-7152-0ed3-0000-261993067432",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067433"
            },
            "type": "SubInterface",
            "vlanId": 599,
            "subIntfId": 599,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "65bb14c6-a344-11ed-b5ed-82df665b6681",
                "type": "SecurityZone"
            },
            "ifname": "V_WAN1",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.225",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_WAN1 599",
            "id": "0050568A-7152-0ed3-0000-261993067433",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067421"
            },
            "type": "SubInterface",
            "vlanId": 100,
            "subIntfId": 100,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "8b7a70c4-a341-11ed-8163-aaca38c2b2bb",
                "type": "SecurityZone"
            },
            "ifname": "V_POS",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.164.253.1",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_POS 100",
            "id": "0050568A-7152-0ed3-0000-261993067421",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067422"
            },
            "type": "SubInterface",
            "vlanId": 101,
            "subIntfId": 101,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "b87085a0-a341-11ed-8163-aaca38c2b2bb",
                "type": "SecurityZone"
            },
            "ifname": "V_EPT",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.164.253.33",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_EPT 101",
            "id": "0050568A-7152-0ed3-0000-261993067422",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067423"
            },
            "type": "SubInterface",
            "vlanId": 102,
            "subIntfId": 102,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "f138e7d8-a341-11ed-b5ed-82df665b6681",
                "type": "SecurityZone"
            },
            "ifname": "V_Duc-E",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.164.253.65",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_Duc-E 102",
            "id": "0050568A-7152-0ed3-0000-261993067423",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067424"
            },
            "type": "SubInterface",
            "vlanId": 103,
            "subIntfId": 103,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "05a509c2-a342-11ed-bed5-bf433dd4499b",
                "type": "SecurityZone"
            },
            "ifname": "V_Printers",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.164.253.97",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_Printers 103",
            "id": "0050568A-7152-0ed3-0000-261993067424",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067425"
            },
            "type": "SubInterface",
            "vlanId": 510,
            "subIntfId": 510,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "1a5b7bc6-a342-11ed-9e21-88bd2c09fdb3",
                "type": "SecurityZone"
            },
            "ifname": "V_Users",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.1",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_Users 510",
            "id": "0050568A-7152-0ed3-0000-261993067425",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067426"
            },
            "type": "SubInterface",
            "vlanId": 530,
            "subIntfId": 530,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "5c6b5b44-a342-11ed-bed5-bf433dd4499b",
                "type": "SecurityZone"
            },
            "ifname": "V_Duc-P",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.65",
                    "netmask": "28"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_Duc-P 530",
            "id": "0050568A-7152-0ed3-0000-261993067426",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067427"
            },
            "type": "SubInterface",
            "vlanId": 535,
            "subIntfId": 535,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "46e533b2-a342-11ed-8163-aaca38c2b2bb",
                "type": "SecurityZone"
            },
            "ifname": "V_Duc-WiFi",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.164.253.129",
                    "netmask": "25"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_Duc-WiFi 535",
            "id": "0050568A-7152-0ed3-0000-261993067427",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067428"
            },
            "type": "SubInterface",
            "vlanId": 540,
            "subIntfId": 540,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "77652f06-a342-11ed-bed5-bf433dd4499b",
                "type": "SecurityZone"
            },
            "ifname": "V_CCTV",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.97",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_CCTV 540",
            "id": "0050568A-7152-0ed3-0000-261993067428",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067429"
            },
            "type": "SubInterface",
            "vlanId": 541,
            "subIntfId": 541,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "bd7fc596-a342-11ed-bed5-bf433dd4499b",
                "type": "SecurityZone"
            },
            "ifname": "V_Tech",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.81",
                    "netmask": "28"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_Tech 541",
            "id": "0050568A-7152-0ed3-0000-261993067429",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        },
        {
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            },
            "links": {
                "self": "https://10.163.1.43/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/devices/devicerecords/adab761e-9668-11ee-abae-e49d35beb127/subinterfaces/0050568A-7152-0ed3-0000-261993067430"
            },
            "type": "SubInterface",
            "vlanId": 550,
            "subIntfId": 550,
            "MTU": 1500,
            "priority": 0,
            "enabled": true,
            "mode": "NONE",
            "managementOnly": false,
            "securityZone": {
                "id": "20c8faae-a344-11ed-9e21-88bd2c09fdb3",
                "type": "SecurityZone"
            },
            "ifname": "V_Servers",
            "enableSGTPropagate": true,
            "ipv4": {
                "static": {
                    "address": "10.165.253.129",
                    "netmask": "27"
                }
            },
            "ipv6": {
                "enableRA": true,
                "enableIPV6": false,
                "enforceEUI64": false,
                "enableAutoConfig": false,
                "enableDHCPAddrConfig": false,
                "enableDHCPNonAddrConfig": false,
                "dadAttempts": 1,
                "nsInterval": 1000,
                "reachableTime": 0,
                "raLifeTime": 1800,
                "raInterval": 200
            },
            "enableAntiSpoofing": true,
            "name": "Port-channel1",
            "description": "V_Servers 550",
            "id": "0050568A-7152-0ed3-0000-261993067430",
            "metadata": {
                "timestamp": 0,
                "state": "COMMITTED"
            }
        }
    ]
}

Hey @ericdavishop :wave:

Welcome to the Postman Community! :postman:

A basic way to do this could be something simple like a loop over the items array and storing the data as the key and value of the variable:

let response = pm.response.json();
_.each(response.items, (item) => {
    pm.globals.set(item.ifname, item.id);
});

I’ve used a Global variable here as an example but you will need to choose the right scope for your context.

You could also use another JS method like map or something similar to achieve the same thing.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.