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");
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
{
“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
Since you haven’t provided the entire response I am giving a wild guess.
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.