So here’s the example using this endpoint: https://datausa.io/api/data?drilldowns=Nation&measures=Population
Which returns,
{
“data”: [
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2019,
“Year”: “2019”,
“Population”: 328239523,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2018,
“Year”: “2018”,
“Population”: 327167439,
“Slug Nation”: “united-states”
},
{
“ID Nation”: “01000US”,
“Nation”: “United States”,
“ID Year”: 2017,
“Year”: “2017”,
“Population”: 325719178,
“Slug Nation”: “united-states”
},
.
.
.
So here’s the code I am using:
pm.test(“Get source_name”, function () {
var jsonData = pm.response.json();
// So this works and returns ==> 328239523
var pop_1 = jsonData.data[0].Population
// → pop_1 returns 328239523
console.log("pop_1: " + pop_1)
pm.expect(pop_1).to.eql(328239523); // SUCCESSFUL
// ***** What I want is to be able to pass in/or set the json path variable
// ***** to “data[0].Population” and be able to still get the value
var jsonPath = “data[0].Population”
//–> I want to be able to just use the jsonData object append to using the jsonPath (which is a string)
// I have tried this but doesn’t work: var pop_2 = eval(jsonData.jsonPath)
// pop_2 returns undefined *** WHAT IS NEEDED TO MAKE THIS WORK: ‘jsonData.jsonPath’
var pop_2 = jsonData.jsonPath
console.log("pop_2: " + pop_2)
// NOT working here since pop_2 is undefined
pm.expect(pop_2).to.eql(328239523); // NOT SUCCESSFUL
});