Extracting a variable from response array

I’m trying to collect a variable from the response array, but can’t access the array/variable in the test script correctly.

I have a list of tasks with their ids stored in a csv file. I’m looping through all the tasks with a request that gives me the checklists and their ids on the store card from Meistertask API.
what I’m struggling with is storing these checklists ids into a variable using:

var bodyData = JSON.parse(responseBody);

value = bodyData.datax[0].id

console.log

There was an error in evaluating the test script: TypeError: Cannot read properties of undefined (reading ‘0’)

I can’t access the array or get the value of the “id”:

this is the response I get from the request https://www.meistertask.com/api/tasks/{{task_id}}/checklists?items=6

[
    {
        "id": 54036003,
        "name": "Broadband",
        "sequence": 0.0,
        "task_id": 114372340,
        "project_id": 6023397
    },
    {
        "id": 54036005,
        "name": "Cabling Site Survey",
        "sequence": 15000.0,
        "task_id": 114372340,
        "project_id": 6023397
    },
    {
        "id": 54036006,
        "name": "Constraints",
        "sequence": 30000.0,
        "task_id": 114372340,
        "project_id": 6023397
    },
    {
        "id": 54036007,
        "name": "Install, Test & Turn-Up",
        "sequence": 45000.0,
        "task_id": 114372340,
        "project_id": 6023397
    },
    {
        "id": 54036008,
        "name": "Porting ",
        "sequence": 60000.0,
        "task_id": 114372340,
        "project_id": 6023397
    },
    {
        "id": 54036010,
        "name": "TEMNS - MES - Day Two Support",
        "sequence": 75000.0,
        "task_id": 114372340,
        "project_id": 6023397
    }
]

I have tried multiple methods on the community posts, but none worked. I tried the JSON path finder but that doesn’t work either.

I want to collect these checklists ids to a csv file so that I can use them into a delete request that will loop through all these ids to delete them as we are trying to get rid of these checklists from the cards in the project.

Hey @mohammedattia

Would it not be something like this to get the first objects id?

bodyData[0].id

bodyData would be the array so the index would be part of that. I’m not really sure where the datax reference was coming from in your response body.

1 Like

that was the format I was following from the JSON pathfinder. that actually worked thanks. any idea on how to store it in a variable now and send it into a csv column
Edit: I figured how to collect the info into a variable. now a matter of how to send it back to the same CSV I’m using to get the task ids

You can use this in the Tests script to set it as a global variable:

pm.globals.set('id', bodyData[0].id)

Other variable scopes could be used, it all depends on the context.

1 Like

Hi.

Let’s say the elements/objects returned in the array can vary.
I can count the array length and display the number of objects.

But:

  • How can we get/display all the ID values from elements returned?

  • How can I verify a certain ID from element number n is as expected?

    Something like (for example):
    console.log(responseBody[0].ID);
    Where n is a variable for each item in the array:
    console.log(responseBody[n].ID);

  • How do you declare it?

Thanks! :slight_smile:

@skyroamofghostviper

This should be its own question.

However, this is an example of looping though an array and testing each element.

It’s uses a JavaScript concept of string literals to customise the test case name.

Based on the following response (which is an array of students).

[
    {
        "id": "1",
        "name": "John",
        "location": "india",
        "phone": "1234567890",
        "courses": [
            "java",
            "selenium"
        ]
    },
    {
        "name": "Raunak",
        "location": "india",
        "phone": "98876543213",
        "courses": [
            "python",
            "appium"
        ],
        "id": "2"
    },
    {
        "id": "3",
        "name": "Smith",
        "location": "Canada",
        "phone": "165498765",
        "courses": [
            "C#",
            "RestAPI"
        ]
    }
]

The following is a test that loops through the response, and checks that the location is a string.

const response = pm.response.json();

console.log(response);

response.forEach(object => {
    pm.test(`location for id${object.id} is a string`, () => {
        pm.expect(object.location).to.be.a("string");
    });
});

image

This way, if one fails, you will know which id its linked to.

This type of activity is covered in the Postman Learning Centre.

If you are new to Postman, then I would suggest a visit to the Learning Centre.

Overview | Postman Learning Center

Including the Postman Training links under “Other Resources”.

image

I would recommend the “Galaxy APIs 101” course first as it gets you used to the application features.

Then the “Galaxy Testing and Automation” course which teaches you how to assert the responses. It includes defining and using variables from responses.

2 Likes