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
});