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