Search code examples
jsonpostmanextractresponse

Extract multiple values from a Json response for a Post request requiring each value


I need to gather an array of all queueCallManagerIDs from the response and build a loop that executes an api call requiring each queueCallManagerID.

I am only able to get the first queueCallManagerID value assigned using code below:

Test:

var jsonData = pm.response.json();
pm.environment.set("queueCallManagerID", jsonData.data[0].queueCallManagerID);

Response:


{
    "count": 3386,
    "status": "Complete",
    "description": "",
    "data": [
        {
            "endTime": "2024-02-23 05:07:52",
            "queueCallManagerID": "79730950",
            "mixmonFileName": "con2526-79730950.wav",
            
        },
        {
            "endTime": "2024-02-23 05:08:18",
            "queueCallManagerID": "79730957",
            "mixmonFileName": "q-con2526-us1-vp-4008-1708664701.1269429.wav",
          
        },

Solution

  • map() will extract all of array data

    const queueCallManagerIDs = jsonData.data.map(item => item.queueCallManagerID);
    

    Get all array data and save into queueCallManagerIDs variable and extract first item and shows all of data by log()

    Demo

    const jsonData = pm.response.json();
    
    // Extract array of queueCallManagerID
    const queueCallManagerIDs = jsonData.data.map(item => item.queueCallManagerID);
    
    pm.environment.set('queueCallManagerIDs', JSON.stringify(queueCallManagerIDs));
    
    console.log("all data: " + pm.environment.get("queueCallManagerIDs"));
    
    const allData = JSON.parse(pm.environment.get("queueCallManagerIDs"));
    if (Array.isArray(allData) && allData.length > 0) {
        const firstData = allData[0];
        console.log("First data: " + firstData);
    } else {
        console.log("No data available in the queueCallManagerIDs variable.");
    }
    

    Result

    enter image description here