Collection Runner with dynamic number of nested keys

I have a collection set up to set up a series of details in bulk using a platform’s API. This platform will contain event information, but the number of events can be dynamic.

The JSON will look similar to the following - note the various “eventdate” keys:

{
{
    "id": "1",
    "name": "first name",
    "timezone": "US/Central",
    "eventdate1": "2023-10-25"
},
{
    "id": "2",
    "name": "second name",
    "timezone": "US/Central",
    "eventdate1": "2023-11-10",
    "eventdate2": "2023-11-11",
    "eventdate3": "2023-11-12"
},
{
    "id": "3",
    "name": "third name",
    "timezone": "US/Eastern",
    "eventdate1": "2023-12-15"
}
}

In some cases, there may be tens of events included.

How can I set up the collection runner in a way where I can loop through eventdates when the number of these keys will be dynamic under each grouping?

What do you want to do with those event dates within the loop?

Is that JSON correct, or should the leading and ending curly braces be square brackets (making it an array)?

Based on what you want to achieve, I suspect it will be an outer forEach loop, then a filter to return the event dates within each object, with another forEach loop that loops over the returned event dates (hence the question on what you want to do with those event dates).

However, you can consider the following as a starter for 10.

const response = pm.response.json();

response.forEach((object) => {

    // Lodash _pick
    let eventDates = _.pick(object, (_value, key, _object) => {
        // simple regex
        return /eventdate/.test(key)
    });

    console.log(eventDates);

    // Second loop potentially goes here.  For example...
    for (const [key, value] of Object.entries(eventDates)) {
        console.log(key, value);
    }

});

image

It’s not that easy to filter on key names via wildcard. To give you an better answer, we sort of need to know what you want to do with those event dates.

Thank you, Mike! The JSON example I listed above is based on a data file I’m feeding into the collection runner, instead of a response I’m receiving back.

ID/Name/Timezone all get passed to an API endpoint as a body response. After that, another call is made to a different API endpoint to retrieve the number of events, and then a third call to another endpoint is made to provide the date information for each event.

That’s the challenge - I know how to run the collection runner through a data file when the key/value pairs are the same for each request, but in this case, the eventdate key is where I’m getting hung up since I’m not sure how to cycle the collection/call when the number of eventdate keys will vary.

Ok, lets start by making the event dates in the data file an array like following.

[
    {
        "id": "1",
        "name": "first name",
        "timezone": "US/Central",
        "eventdates": [
            {
                "eventdate1": "2023-10-25"
            }
        ]
    },
    {
        "id": "2",
        "name": "second name",
        "timezone": "US/Central",
        "eventdates": [
            {
                "eventdate1": "2023-11-10"
            },
            {
                "eventdate2": "2023-11-11"
            },
            {
                "eventdate3": "2023-11-12"
            }
        ]
    },
    {
        "id": "3",
        "name": "third name",
        "timezone": "US/Eastern",
        "eventdates": [
            {
                "eventdate1": "2023-12-15"
            }
        ]
    }
]

You will want to retrieve this array in one of the earlier requests in the flow and then save it as a collection or environment variable. You can target the array through the special data variable that gives you access to the content of your data files.

pm.collectionVariables.set("eventdates", JSON.stringify(data.eventdates));

Remember to JSON.stringify when storing arrays and JSON.parse when retrieving.

In the pre-request script of the request you want to loop, you will then retrieve this array (using JSON.parse) and use an method available to arrays called shift();

What this does is retrieve the first element from the array, and also remove it from the array.

You will then save that element to the variable (eventdate) you want to use in the specific request, and you will also re-save the array (which will now have one less item).

let array = JSON.parse(pm.collectionVariables.get("testdates"));
let testdate = array.shift();
pm.collectionVariables.set("testdate",testdate);
pm.collectionVariables.set("testdates", JSON.stringify(array));

In the tests tab for the request, you will retrieve the array again as you need to check the length and will have an IF statement that keeps looping using setNextRequest while the array length is more than zero.

const response = pm.response.json();

let array = JSON.parse(pm.collectionVariables.get("testdates"));

let requestName = pm.info.requestName;

if (array.length > 0) {
    postman.setNextRequest(requestName);

} else {
    postman.setNextRequest(null);
}

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