Response Arrays

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 :

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

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

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

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:

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

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

{
    "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"
    }
}

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

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

Finally, Postman uses JavaScript under the hood, If you are going to be scripting, I would also recommend learning some JavaScript basics. WC3 schools is a good place to start.

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

// 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).

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

Thank you ! Appreciate this a lot as a newbie !

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