How to extract a specific value from an array from a collection variable

How do I extract the array values in the collection variable?

I used this script to capture the values I would like to store in the collection variable array:

pm.test("Armazenando valores de response", function () {

    //criando variável na collection
    let responses = pm.collectionVariables.get('infoCount')

    if(responses) {
    responses = JSON.parse(responses);
    } else {
    responses = []
    }

    //setando valores na variável
    responses.push(pm.response.json().info.count); 
    pm.collectionVariables.set('infoCount', JSON.stringify(responses));

    //visualizando o primeiro item do array response.message
    console.log(responseVerifyUser)

});

The value in the collection variable:
image

I tried extracting the values as follows:

let responseTest = pm.collectionVariables.get('infoCount')
console.log(responseTest)

The result then was this
image

however when I try to get the value of the index [0] the result looks like this
console.log(responseTest[0])
image

[1] = “8”
[2] = “2”
[3] = “6”
[4] = “]”

How then do I extract from the index array [0] = 826, [1] = 827 … from the collection variable? (and not in this “broken” way that is coming)

As you can see, “[826,827” is a string, so when you type responseTest[0], it is expected behavior to get the “[”. By not using JSON.stringify, you can actually get what you want.

it’s working, thanks @semihberkay

1 Like