# Response Arrays

**URL:** <https://community.postman.com/t/response-arrays/48462>\
**Category:** Help Hub\
**Tags:** tests, collections, scripts\
**Created:** [June 13, 2023, 7:07am UTC](https://community.postman.com/t/response-arrays/48462 "2023-06-13T07:07:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![flight-specialist-33](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flight-specialist-33/32/15395_2.png) [@flight-specialist-33](https://community.postman.com/u/flight-specialist-33)\
**Post date:** [June 13, 2023, 7:07am UTC](https://community.postman.com/t/response-arrays/48462/1 "2023-06-13T07:07:55Z")

</div>

How do I get the variable from an array and use it for another request?

I’ve used the test scripts and it worked for stand alone variables in the response.

How does one do if the variable comes in an array ?

Capture and store as an environment variable to be used in another request?

Sample response i’m using :

```auto
{
    "identifiers": [
        {
            "typeCode": "SID",
            "list": 
            [
                "8929135821"
            ]
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 13, 2023, 11:27am UTC](https://community.postman.com/t/response-arrays/48462/2 "2023-06-13T11:27:16Z")

</div>

This is how to target the elements in your example response.

```auto
const response = pm.response.json();

console.log(response);

// identifiers is an array
// array indexes start at 0
console.log(response.identifiers[0]);

// To target the typeCode element within the array
console.log(response.identifiers[0].typeCode);

// List is also an array
console.log(response.identifiers[0].list[0]);

// set environment variables the normal way
pm.environment.set("list", response.identifiers[0].list[0]);

```

**Console logs**

![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/2/72e41560775c9ec89a9b2a6a98582f7db45b50a3.png)

---

<div class="post-metadata">

**Author:** ![flight-specialist-33](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flight-specialist-33/32/15395_2.png) [@flight-specialist-33](https://community.postman.com/u/flight-specialist-33)\
**Post date:** [June 15, 2023, 1:17am UTC](https://community.postman.com/t/response-arrays/48462/3 "2023-06-15T01:17:26Z")

</div>

Thanks MDJones.  
It worked and i have expanded it.

One more question,

I need that value from the ‘list’ array to be assigned to this request object:

```auto
"identifiers": 
    [
        {
            "typeCode": "shipmentId",
            "value": "8929135821"
        }
    ],

```

How is it done in the POST request ? It’s a different request. Complete structure below.

```json
{
    "plannedShippingDateAndTime": "2022-12-05T14:00:31GMT+08:00",
    "pickup": {
        "isRequested": false
    },
    "productCode": "P",
    "accounts": [
        {
            "typeCode": "shipper",
            "number": ""
        }
    ],
    "valueAddedServices": [
        {
            "serviceCode": "YW"
        }
    ],
    "outputImageProperties": {
        "printerDPI": 300,
        "encodingFormat": "pdf",
        "imageOptions": [
            {
                "typeCode": "label",
                "templateName": "ECOM26_84_001"
            },
            {
                "typeCode": "waybillDoc",
                "templateName": "ARCH_8X4",
                "isRequested": true,
                "numberOfCopies": 1
            }
        ]
    },
    "identifiers": [
        {
            "typeCode": "shipmentId",
            "value": "2259707380"
        }
    ],
    "customerDetails": {
        "shipperDetails": {
            "postalAddress": {
                "postalCode": "75350",
                "cityName": "BATU BERENDAM",
                "countryCode": "MY",
                "addressLine1": "LOT 6, BATU BERENDAM FTZ PHASE 3",
                "addressLine2": ".",
                "addressLine3": "."
            },
            "contactInformation": {
                "phone": "+6062833566",
                "companyName": "DOMINANT OPTO TECHNOLOGIES SDN BHD",
                "fullName": "FAISAL"
            }
        },
        "receiverDetails": {
            "postalAddress": {
                "postalCode": "04435",
                "cityName": "FLUGHAFEN LEIPZIG/HALLE",
                "countryCode": "DE",
                "addressLine1": "HERMANN-KOEHL-STRASSE 1, SCHKEUDITZ",
                "addressLine2": "LEIPZEIG 04435 GERMANY",
                "addressLine3": "."
            },
            "contactInformation": {
                "phone": "+4972648901020",
                "companyName": "DOMINANT SEMICONDUCTORS EUROPE GMBH",
                "fullName": "BBX DEPARTMENT C/O DHL EXPRESS HUB"
            }
        },
        "importerDetails": {
            "postalAddress": {
                "postalCode": "04435",
                "cityName": "FLUGHAFEN LEIPZIG/HALLE",
                "countryCode": "DE",
                "addressLine1": "HERMANN-KOEHL-STRASSE 1, SCHKEUDITZ",
                "addressLine2": "LEIPZEIG 04435 GERMANY",
                "addressLine3": "."
            },
            "contactInformation": {
                "phone": "+4972648901020",
                "companyName": "DOMINANT SEMICONDUCTORS EUROPE GMBH",
                "fullName": "BBX DEPARTMENT C/O DHL EXPRESS HUB",
                "email": "andreas.mugeto@dominant-semi.eu"
            },
            "registrationNumbers": [
                {
                    "typeCode": "VAT",
                    "number": "DE258381072",
                    "issuerCountryCode": "DE"
                },
                {
                    "typeCode": "EOR",
                    "number": "07140207",
                    "issuerCountryCode": "DE"
                }
            ]
        }
    },
    "content": {
        "packages": [
            {
                "weight": 400.0,
                "dimensions": {
                    "length": 35,
                    "width": 35,
                    "height": 35
                },
                "identifiers": [
                    {
                        "typeCode": "pieceId",
                        "value": "JD014600004600348842"
                    }
                ]
            },
            {
                "weight": 400.0,
                "dimensions": {
                    "length": 35,
                    "width": 35,
                    "height": 35
                },
                "identifiers": [
                    {
                        "typeCode": "pieceId",
                        "value": "JD014600004600348843"
                    }
                ]
            },
            {
                "weight": 400.0,
                "dimensions": {
                    "length": 35,
                    "width": 35,
                    "height": 35
                },
                "identifiers": [
                    {
                        "typeCode": "pieceId",
                        "value": "JD014600004600348844"
                    }
                ]
            }
        ],
        "isCustomsDeclarable": true,
        "declaredValue": 3000.00,
        "declaredValueCurrency": "USD",
        "exportDeclaration": {
            "lineItems": [
                {
                    "number": 1,
                    "description": "LIGHT EMITTING DIODES (LED)",
                    "price": 0.03700,
                    "priceCurrency": "USD",
                    "quantity": {
                        "value": 7000,
                        "unitOfMeasurement": "PCS"
                    },
                    "commodityCodes": [
                        {
                            "typeCode": "inbound",
                            "value": "8541.11.00000"
                        }
                    ],
                    "exportReasonType": "permanent",
                    "manufacturerCountry": "MY",
                    "weight": {
                        "netValue": 20.3,
                        "grossValue": 20.3
                    }
                },
                {
                    "number": 1,
                    "description": "LIGHT EMITTING DIODES (LED)",
                    "price": 0.03700,
                    "priceCurrency": "USD",
                    "quantity": {
                        "value": 8000,
                        "unitOfMeasurement": "PCS"
                    },
                    "commodityCodes": [
                        {
                            "typeCode": "inbound",
                            "value": "8541.11.00000"
                        }
                    ],
                    "exportReasonType": "permanent",
                    "manufacturerCountry": "MY",
                    "weight": {
                        "netValue": 20.3,
                        "grossValue": 20.3
                    }
                },
                {
                    "number": 1,
                    "description": "LIGHT EMITTING DIODES (LED)",
                    "price": 0.03700,
                    "priceCurrency": "USD",
                    "quantity": {
                        "value": 7000,
                        "unitOfMeasurement": "PCS"
                    },
                    "commodityCodes": [
                        {
                            "typeCode": "inbound",
                            "value": "8541.11.00000"
                        }
                    ],
                    "exportReasonType": "permanent",
                    "manufacturerCountry": "MY",
                    "weight": {
                        "netValue": 20.3,
                        "grossValue": 20.3
                    }
                }
            ],
            "invoice": {
                "number": "BBS2010131",
                "date": "2022-05-30"
            },
            "placeOfIncoterm": "FLUGHAFEN LEIPZIG/HALLE",
            "exportReasonType": "permanent"
        },
        "description": "LIGHT EMITTING DIODES (LED)",
        "incoterm": "DAP",
        "unitOfMeasurement": "metric"
    }
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 15, 2023, 11:56am UTC](https://community.postman.com/t/response-arrays/48462/4 "2023-06-15T11:56:00Z")

</div>

If you are new to Postman I would recommend the Postman training links which are located under “other resources” in the Learning Centre.

[Other resources | Postman Learning Center](https://learning.postman.com/docs/introduction/resources/)

The “Galaxy API’s 101” course gets you used to sending requests and the GUI.

The “Galaxy Testing and Automation” gets you used to testing your responses and using variables, basic scripting, etc.

Also take a look at the following.

[Postman JavaScript reference | using-environment-variables-in-scripts](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-environment-variables-in-scripts)

Finally, Postman uses JavaScript under the hood, If you are going to be scripting, I would also recommend learning some JavaScript basics. [WC3 schools](https://www.w3schools.com/) is a good place to start.

To answer your particular question. As you have already set an environment variable for that value here…

```auto
// set environment variables the normal way
pm.environment.set("list", response.identifiers[0].list[0]);

```

You can just reference the variable it in the body using handlebars (aka curly brackets).

```auto
"identifiers": [
    {
        "typeCode": "shipmentId",
        "value": {{list}}
    }
]

```

---

<div class="post-metadata">

**Author:** ![flight-specialist-33](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flight-specialist-33/32/15395_2.png) [@flight-specialist-33](https://community.postman.com/u/flight-specialist-33)\
**Post date:** [June 22, 2023, 12:50am UTC](https://community.postman.com/t/response-arrays/48462/5 "2023-06-22T00:50:32Z")

</div>

Thank you ! Appreciate this a lot as a newbie !

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [January 26, 2024, 5:02pm UTC](https://community.postman.com/t/response-arrays/48462/6 "2024-01-26T17:02:34Z")

</div>

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