I am trying to count the values a json array.
I want to count the number of id's in the data array of "sierra" if "beta = b". Hence checking the value of data[].beta against the environment variable ("B") set to value 'b'.
The issue here is I do not have "sierra" in every iteration of data[].
{
"data": [{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "a"
},
{
"type": "alphabet",
"id": "b"
}
]
}
}
},
{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"bravo": {
"data": [
{
"type": "number",
"id": "1"
},
{
"type": "number",
"id": "2"
}
]
}
}
},
{
"alpha": "x",
"beta": "y",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "c"
},
{
"type": "alphabet",
"id": "d"
}
]
}
}
}]
}
Above json is the response body I see in postman. "loop" is the count of my "for" loop.
EDIT 1: I've tried this:
1. pm.response.json().data[loop].gamma.sierra.data().id).size()
2. for(var loop =0; loop < pm.response.json().data.length; loop++)
{
if(pm.response.json().data[loop].beta===pm.variables.get("B"))
{
pm.response.json().data.map((item, loop) => {
if(item.gamma){ // check if gamma key is present
console.log(item.gamma.sierra.filter(data =>data.id
).length); //
}
});
result=true;
break;
}
}
pm.expect(true).to.eql(result);
Expected: 2
Actual: TypeError: Cannot read property 'sierra' of undefined
Actual: item.relationships.apps.filter is not a function