Need to verify multiple value pairs

My question: I want to verify that in year 2020 , the population was 326569308 in the first response below. how i can create the test for this with postman

Details (like screenshots):

Json Response:

{
“data”: [
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2020,
“Year”: “2020”,
“Population”: 326569308,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2019,
“Year”: “2019”,
“Population”: 324697795,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2018,
“Year”: “2018”,
“Population”: 322903030,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2017,
“Year”: “2017”,
“Population”: 321004407,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2016,
“Year”: “2016”,
“Population”: 318558162,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2015,
“Year”: “2015”,
“Population”: 316515021,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2014,
“Year”: “2014”,
“Population”: 314107084,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2013,
“Year”: “2013”,
“Population”: 311536594,
“Slug Nation”: “united-states”
}
],
“source”: [
{
“measures”: [
“Population”
],
“annotations”: {
“source_name”: “Census Bureau”,
“source_description”: “The American Community Survey (ACS) is conducted by the US Census and sent to a portion of the population every year.”,
“dataset_name”: “ACS 5-year Estimate”,
“dataset_link”: “American Community Survey (ACS)”,
“table_id”: “B01003”,
“topic”: “Diversity”,
“subtopic”: “Demographics”
},
“name”: “acs_yg_total_population_5”,
“substitutions”:
}
]
}

How I found the problem:
I am new to this and still learning

I’ve already tried:

i have tried this by
var jsonData = pm.response.json();
for (subArray of jsonData){
for (subSubArray of subArray){
if (subSubArray.data.Year == ‘2020’){
pm.test(“population in 2020 was 326569308”, ()=>{
pm.expect(subSubArray.data.Population).to.eql(326569308)
})
}
}
but it is giving me an error TypeError: jsonData is not iterable

Hi @07se81

Your response isn’t an array, the array is actually under “data”.

Try changing your first line to this;

var jsonData = pm.response.json().data;

I use a helper function for searching JavaScript objects and returning an array of the results.

It’s so much easier to assert on.

var jsonData = {

"data": [
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2020,
"Year": "2020",
"Population": 326569308,
"Slug Nation": "united-states"
},
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2019,
"Year": "2019",
"Population": 324697795,
"Slug Nation": "united-states"
},
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2018,
"Year": "2018",
"Population": 322903030,
"Slug Nation": "united-states"
},
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2017,
"Year": "2017",
"Population": 321004407,
"Slug Nation": "united-states"
},
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2016,
"Year": "2016",
"Population": 318558162,
"Slug Nation": "united-states"
},
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2015,
"Year": "2015",
"Population": 316515021,
"Slug Nation": "united-states"
},
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2014,
"Year": "2014",
"Population": 314107084,
"Slug Nation": "united-states"
},
{
"ID Nation": "01000US",
"Nation": "United States",
"ID Year": 2013,
"Year": "2013",
"Population": 311536594,
"Slug Nation": "united-states"
}
],
"source": [
{
"measures": [
"Population"
],
"annotations": {
"source_name": "Census Bureau",
"source_description": "The American Community Survey (ACS) is conducted by the US Census and sent to a portion of the population every year.",
"dataset_name": "ACS 5-year Estimate",
"dataset_link": "American Community Survey (ACS)",
"table_id": "B01003",
"topic": "Diversity",
"subtopic": "Demographics"
},
"name": "acs_yg_total_population_5",
"substitutions": ""
}
]
}

//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));    
        } else 
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key && obj[i] == val || i == key && val == '') { //
            objects.push(obj);
        } else if (obj[i] == val && key == ''){
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1){
                objects.push(obj);
            }
        }
    }
    return objects;
}

// creates array of the search results
var search = getObjects(jsonData,'ID Year','2020');

pm.test("Population in 2020 was 326569308", () => {
   pm.expect(search[0].Population).to.eql(326569308);
});

// or
var Population = getObjects(jsonData,'ID Year','2020')[0].Population;
console.log(Population);

pm.test("Population in 2020 was 326569308 V2", () => {
   pm.expect(Population).to.eql(326569308);
});