# Build Comma Seperated Variable from Response Body

**URL:** <https://community.postman.com/t/build-comma-seperated-variable-from-response-body/9046>\
**Category:** Help Hub\
**Tags:** variables, json\
**Created:** [November 16, 2019, 10:06pm UTC](https://community.postman.com/t/build-comma-seperated-variable-from-response-body/9046 "2019-11-16T22:06:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![REST\_in\_Space](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rest_in_space/32/3317_2.png) [@REST\_in\_Space](https://community.postman.com/u/REST_in_Space)\
**Post date:** [November 16, 2019, 10:06pm UTC](https://community.postman.com/t/build-comma-seperated-variable-from-response-body/9046/1 "2019-11-16T22:06:18Z")

</div>

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,

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 17, 2019, 8:33am UTC](https://community.postman.com/t/build-comma-seperated-variable-from-response-body/9046/2 "2019-11-17T08:33:46Z")

</div>

Hey @REST_in_Space,

Welcome to the Postman community!! 🚀

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. 🙂

It’s basically using the Lodash [\_.map()](https://lodash.com/docs/4.17.15#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()](https://www.w3schools.com/jsref/jsref_split.asp) 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.

 ![00](https://us1.discourse-cdn.com/getpostman/original/2X/f/fe45d825096aa8144f7572782641abc0c843a0fa.png)

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}}
            ]
        }
    }
}

```

 ![28](https://us1.discourse-cdn.com/getpostman/original/3X/2/2/2292ebe561c24e66c13218c4f8e40879b749b7c7.png)

Hope that helps 🙂

---

<div class="post-metadata">

**Author:** ![REST\_in\_Space](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rest_in_space/32/3317_2.png) [@REST\_in\_Space](https://community.postman.com/u/REST_in_Space)\
**Post date:** [November 19, 2019, 4:35pm UTC](https://community.postman.com/t/build-comma-seperated-variable-from-response-body/9046/3 "2019-11-19T16:35:43Z")

</div>

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.

 ![2](https://us1.discourse-cdn.com/getpostman/original/3X/c/5/c508c6c74383357300f2059edfb0a9de4dcc6064.png)

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

 ![3](https://us1.discourse-cdn.com/getpostman/original/3X/3/e/3e8131568577265ac490bf1a84fc7a1716a23f6d.png)

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.

 ![1](https://us1.discourse-cdn.com/getpostman/original/2X/b/b693a418ad7fcf3f8ec9acb5f88e1720f3c36caf.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 19, 2019, 5:25pm UTC](https://community.postman.com/t/build-comma-seperated-variable-from-response-body/9046/4 "2019-11-19T17:25:27Z")

</div>

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:

 ![32](https://us1.discourse-cdn.com/getpostman/original/3X/0/2/02dec6ad30ef456b948993fbee9820f07b0b4c79.png)

---

<div class="post-metadata">

**Author:** ![REST\_in\_Space](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rest_in_space/32/3317_2.png) [@REST\_in\_Space](https://community.postman.com/u/REST_in_Space)\
**Post date:** [November 21, 2019, 3:11pm UTC](https://community.postman.com/t/build-comma-seperated-variable-from-response-body/9046/5 "2019-11-21T15:11:08Z")

</div>

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

Everything works flawlessly now.
