Build Comma Seperated Variable from Response Body

I have some JSON data in a response body after I make a request.

I want to take a specific part of this data and add it to a variable to be used in the body of the next request.

JSON Data:

{
    "targets": {
        "@uri": "/api/space/tag-management/tags/98304/targets",
        "target": [
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98328",
                "@href": "/api/space/device-management/devices/196657"
            },
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98329",
                "@href": "/api/space/device-management/devices/196656"
            },
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98330",
                "@href": "/api/space/device-management/devices/196655"
            },
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98331",
                "@href": "/api/space/device-management/devices/196654"
            },
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98332",
                "@href": "/api/space/device-management/devices/196653"
            },
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98333",
                "@href": "/api/space/device-management/devices/196652"
            },
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98334",
                "@href": "/api/space/device-management/devices/196651"
            },
            {
                "@type": "vnd.net.juniper.space.device-management.device",
                "@uri": "/api/space/tag-management/tags/98304/targets/98335",
                "@href": "/api/space/device-management/devices/196650"
            },

The data I need from this body is the number always at the end of the @href.
This is the Device ID i will need for my next request.

I need to store it as 1,2,3,4 or 1, 2, 3, 4, or
1,
2,
3,
4,

This is a sample of the final product without a variable:

{
    "compare-config-by-devices-request": {
        "device-ids": {
            "device-id": [
                197718,
                197710,
                198162,
                198257,
                197761,
                198115,
                197943,
                198218,
                198504,
                197763,
                197940,
                197960,
                198248,
                197967,
                197944,
                198076,

Hey @REST_in_Space,

Welcome to the Postman community!! :rocket:

To get those Id’s and set them as an global variable, you could do something like this in the Tests tab:

let body = pm.response.json().targets.target,
    ids = _.map(body, (id) => id['@href'].split("/")[5]);

pm.globals.set("ids", ids) 

This looks a little odd but I’ll explain what’s happening here. :slight_smile:

It’s basically using the Lodash _.map() function to create a new array from the @href value.

As the id you require is at the end of the string, I’m using the split() method and have added the / character as the separator value, which creates a new array.

That array has 6 items, we need the last item and as it’s a zero-indexed array, we need to add [5] to get that item.

Once those ids are stored using the pm.globals.set() function, you would be able to use them in the Request Body, of the next request, like this:

{
    "compare-config-by-devices-request": {
        "device-ids": {
            "device-id": [
            	{{ids}}
            ]
        }
    }
}

Hope that helps :slight_smile:

1 Like

This is REALLY good and helped a lot.
Every part was flawless except when i try to call the {{ids}} variable it doesn’t work.

In mine i’ve called it DeviceID.

It grabs it perfectly.
But gives me this error when i try to implement it as instructed. (Maybe i missed something?)

I think it just doesn’t recognize the Variable call as when i copy the contents of the variable and paste them in, it works just fine.

Do you have the same variable name set at a different level (Collection, Environment, etc) - The Current Value is crossed out which would suggest that it is.

When you use the {{DeviceId}} syntax, when does the Postman Console say about what is sent in the request body?

You can get to the console by pressing this icon:

Thanks a lot.
I found the same thing when looking and corrected it.

Everything works flawlessly now.

1 Like