Creating assertion to "numeric" object

Hello everybody,

I have a problem with creating assertions regarding to JSON response:

{
    "8888": [
        {
            "name": "Jerzy",
            "Id": 10
        }
    ]
}

As you can see the first object is a “numeric” string. I don’t know how to get to this object.
If I will have for example normal string like: “list” then assertion should be:

var jsonData = pm.response.json();
pm.expect(jsonData.list.name).to.eql("Jerzy");

Anybody can help me with this issue?

1 Like

@lunar-module-meteor3 Welcome to the Community :partying_face:

Yes, I feel you… You need to think of using a bracket notation to access the elements instead of dot notation.

You can see the similar thread here to learn more about it.

In your case it should be something like,

pm.test("Value is as expected", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData['8888'][0]['name']).to.eql("Jerzy");
    pm.expect(jsonData['8888'][0]['Id']).to.eql(10);
});

If this is just a subset of response, please update the path to reach the elements! I hope this is helpful :blush:

Thank you for your answer.
Yes, it’s working.

1 Like

@lunar-module-meteor3 Good to know :partying_face: Do you mind sparing a minute and marking the solution? That would be helpful for the people in future :raised_hands:

1 Like

please @bpricilla help me with this question

{
“data”: {
“carteiras”: [
{
“data_atualizacao”: “2021-07-09T09:42:06.3275534”,
“valor_total_carteira_formatado”: “R$ 50.000,00”,
“uid”: “”,
“id_tagueamento”: “”,
“acessibilidade”: {
“mensagem_atualizacao”: “Última atualização nove de julho de dois mil e vinte e um às nove horas e quarenta e dois minutos”
},
“mensagem_atualizacao”: “Última atualização 09/07/21 às 09:42”,
“conta”: “”,
“nome”: “”,
“tipo_carteira”: “”,
“status”: {
“tipo”: “”,
“mensagem”: “”
}
},
{
“data_atualizacao”: “2021-07-09T09:42:06.3274604”,
“valor_total_carteira_formatado”: “R$ 0,00”,
“uid”: “3660_45wN”,
“id_tagueamento”: “bob_xpmobile”,
“acessibilidade”: {
“mensagem_atualizacao”: “Atualizando conta. Vamos te avisar quando estiver pronto.”
},

pm.test(“Value is as expected”, function () {
var jsonData = pm.response.json();
pm.expect(JSON.data.carteiras[1].acessibilidade.mensagem_atualizacao).to.eql(“Atualizando conta. Vamos te avisar quando estiver pronto.”);

});

FAIL
Value is as expected | ReferenceError: carteiras is not defined

@rosedias Welcome to the community :partying_face:

Since you haven’t provided the entire response I am giving a wild guess.

  1. It should the variable used and you stored the parsed results not as “JSON”
pm.test(“Value is as expected”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data.carteiras[1].acessibilidade['mensagem_atualizacao']).to.eql(“Atualizando conta. Vamos te avisar quando estiver pronto.”);

});

As a beginner whenever you are facing issues in JSON parsing, try to use console.log and see how the entire response looks like.

console.log(jsonData);

or you can use https://jsonpathfinder.com/

Also I have written a blog about this here.

I hope this helps :blush:

:heart_eyes: :heart_eyes: excellent solved

1 Like