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

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

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:

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);

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.

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