# How to create global of int id using code in Tests?

**URL:** <https://community.postman.com/t/how-to-create-global-of-int-id-using-code-in-tests/5581>\
**Category:** Help Hub\
**Tags:** global, variables, automation, postman, tests\
**Created:** [May 24, 2019, 7:56am UTC](https://community.postman.com/t/how-to-create-global-of-int-id-using-code-in-tests/5581 "2019-05-24T07:56:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sero.barseghyan](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sero.barseghyan](https://community.postman.com/u/sero.barseghyan)\
**Post date:** [May 24, 2019, 7:56am UTC](https://community.postman.com/t/how-to-create-global-of-int-id-using-code-in-tests/5581/1 "2019-05-24T07:56:28Z")

</div>

Hi. This is my response. There are _Items_ and _Photos_. I need to create global from _photos_ \> _id_ to have that photo id’s value as global to use in my next request.

```
{
    "status": "success",
    "id": 296320965219101,
    "title": "New Private Collection.",
    "desc": "This is my private collection",
    "total": 1,
    "public": false,
    "user": {
        "id": 29348278687687661,
        "username": "jack.smith",
        "name": "Jack Smith",
    },
    "items": [
        {
            "id": 2880058789753201,
            "url": "https://picture.example.jpg",
            "title": "#NeonLights",
            "width": 1080,
            "height": 1080,
            "mature": false,
            "public": true,
            "likes_count": 0,
            "views_count": 27,
            "reposts_count": 0,
            "comments_count": 0,
            "sources": [],
            "user_id": 989089889989,
            "is_reposted": false
        }
    ],
    "photos": [
        {
            "id": 16127871367813,
            "url": "https://picture.example.jpg",
            "title": "#NeonLights",
            "width": 1080,
            "height": 1080,
            "mature": false,
            "public": true,
            "likes_count": 0,
            "views_count": 27,
            "reposts_count": 0,
            "comments_count": 0,
            "sources": [],
            "user_id": 287958404019101,
            "is_reposted": false
        }
    ],
    "metadata": {}
}

```

I use this code in my pre-request to parse the id into string  
`pm.globals.set("photo collection id_parsed" , parseInt(pm.globals.get("photo collection id ")) )`

And in Tests I use this code to create global for _id_ from _photos_ section.

```
let jsonData_collection_Photoid = pm.response.json();
  pm.globals.set("photo collection id", jsonData_collection_Photoid.photos.id);

```

But I don’t get any saved global after sending the request, please recommend some solution

 ![Screenshot%20at%20May%2024%2011-33-42](https://us1.discourse-cdn.com/getpostman/original/3X/e/d/edd6d9f16b01fc4e8f307067673b481fe39617ac.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:** [May 24, 2019, 1:34pm UTC](https://community.postman.com/t/how-to-create-global-of-int-id-using-code-in-tests/5581/2 "2019-05-24T13:34:50Z")

</div>

Hey @sero.barseghyan

Within your response body, the `photos` property _type_ is an `array`, which contains an `object`. The reference to the `id` value in your code is nearly correct but you need to specify which **object** in the array you want.

You would do this by passing an index number, arrays are zero indexed so the first item in the array would be `0`.

Your code would need to look like this:

```
let jsonData_collection_Photoid = pm.response.json();
pm.globals.set("photo collection id", jsonData_collection_Photoid.photos[0].id);

```

I’ve written more about accessing JSON response data in this post:

> [@Sharing tips and tricks with others in the Postman community](https://community.postman.com/t/sharing-tips-and-tricks-with-others-in-the-postman-community/5123/5):
>
> Extracting values from a JSON response I notice lots of questions from the community which all have a similar flavour - Users are having trouble extracting data from a JSON response body when the value is within an array. These are normally JavaScript related questions, more than specific Postman questions but I want to use this thread to offer more support around this problem. For these examples I’ll be using the [https://randomuser.me](https://randomuser.me) API, this returns lots of random user data, in a JSON form…

---

<div class="post-metadata">

**Author:** ![sero.barseghyan](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sero.barseghyan](https://community.postman.com/u/sero.barseghyan)\
**Post date:** [June 3, 2019, 1:42pm UTC](https://community.postman.com/t/how-to-create-global-of-int-id-using-code-in-tests/5581/3 "2019-06-03T13:42:31Z")

</div>

I still get empty global value using this code.  
let jsonData\_collection\_Photoid = pm.response.json();  
pm.globals.set(“photo collection id”, jsonData\_collection\_Photoid.photos[0].id);

---

<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:** [June 3, 2019, 1:54pm UTC](https://community.postman.com/t/how-to-create-global-of-int-id-using-code-in-tests/5581/4 "2019-06-03T13:54:24Z")

</div>

Hey @sero.barseghyan,

It looks like the `quotes` you have in the global variable name are not correct. This may have come from copy and pasting the text across different places.

I mocked out your response data and ran this to set the variable value.

 ![26](https://us1.discourse-cdn.com/getpostman/original/3X/2/1/2157efb66ec22b225f70d5bd0e0eae224b2ed63d.png)

---

<div class="post-metadata">

**Author:** ![sero.barseghyan](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sero.barseghyan](https://community.postman.com/u/sero.barseghyan)\
**Post date:** [June 3, 2019, 1:59pm UTC](https://community.postman.com/t/how-to-create-global-of-int-id-using-code-in-tests/5581/5 "2019-06-03T13:59:50Z")

</div>

> [@sero.barseghyan](#):
>
> `pm.globals.set("photo collection id_parsed" , parseInt(pm.globals.get("photo collection id ")) )`

Now it’s working. But I see that this code  
pm.globals.set(“photo collection id\_parsed” , parseInt(pm.globals.get("photo collection id ")) )  
is no longer needed in pre-request section
