Is it possible to pass a variable in a JsonPath?

Hello,

It is possible to pass a variable in a json path? For example:

I want to dynamically pass a variable to path below by manipulating ā€˜str2ā€™ either through environment or global variables.

let output2 = jsonData.p.str2.value

Thank IA!

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Has anyone found a solution to this?

Here is a code sample of how to update a path variable dynamically.

So the original question and what we are asking is that if the json response is
{
States:
[
State:
Colorado:
Capital: Denver
]
}

And we get the response
JsonData = pm.response.json()

And I have a variable with jsonpath
cap = State[0].Colorado.Capital

And I want to set its variable to:
let output = jsonData.cap

This doesnā€™t work. How do I get variable ā€˜outputā€™ to get the value Denver??

I used the eval(JsonData.cap) but the eval() doesnā€™t work

Hey!
In order to better help you, could you post a screenshot or cleanly formatted code sample?

The example you gave does not appear to be valid JSON.
Generally you would need to get the index of the object where state = ā€œColoradoā€ from your ā€œStatesā€ array, and then just set your output variable to array[indexOfCollorado].Capital

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

Thank you for providing this!
Is there a reason why it must be a string/path variable? Seems to make things more difficult if we are parsing it back into json right away.

We can do this a few ways, one would be passing ā€œJsonDataā€ as a string in the eval, ie:
var pop_2 = eval("jsonData."+jsonPath);
console.log("pop_2: " + pop_2); // Returns 328239523

Let me know if that works! :smile:

1 Like

Thanks Kevin the code eval(ā€œjsonData.ā€ + jsonPath) works! Thanks for all your help!!!

The reason Iā€™m using this is so that I want to just create one postman request that you can feed in the URL, and get the JSONPATH and EXPECTED_VALUE value so that you can implement a data driven using a csv that contains for example the below sample data so that you can just have one test that grabs the JSONPATH and EXPECTED_VALUE and compare them. Having this data driven approach, the tester can just create the csv file and the postman can just loop through each of these rows when I run it from the command line using newman.

URL,JSONPATH,EXPECTED_VALUE
https://datausa.io/api/data?drilldowns=Nation&measures=Population,data[0].Population,328239523

https://mykitchten.com/food,type.food[0].sauce,mariana

https://datausa.io/api/data?drilldowns=Nation&measures=Nation,data[0].Nation,United States

1 Like

Kevin, so I am have a csv file: exampledata.csv
url,jsonpath,expectedvalue
http://abc.com/car,model[0].type,toyota
http://xyz.com/fruit,type.fruit,banana

So i run command: newman run deom.postman_collection.json -e .demo.postman_environment.json --iteration-data exampledata.csv

So inside my postman request, how do I access the value for url, jsonpath, expectedvalue inside the Pre-requisite-script or Test section?

I tried: var urlValue = pm.iterationData.get(ā€œurlā€) but itā€™s returning undefined? Any help would be appreciated.

Iā€™m afraid I would be of little help here. I am not too experienced with newman just yet.
However, Iā€™ve found some documentation that may help:

I would recommend making sure that the data files are being used correctly with the correct fields in exampledata.csv (url,jsonpath,expectedvalue)

Kevin,
Thanks for all the help, but I figured it out. Iā€™m going to share with you the answer so that in the future you can help someone who has the same problem. Hope this is useful!

  1. In Postman, created generic request like this:
    image.png

  2. Create a csv file like this named: mydata.csv with content:

URL,JSONPATH,EXPECTEDVALUE
https://datausa.io/api/data?drilldowns=Nation&measures=Population,data[0].Population,328239523
https://swapi.dev/api/people/1/,name,Luke Skywalker
https://jsonplaceholder.typicode.com/todos/1,title,delectus aut autem

  1. Run newman command like this:
    newman run .\CSVDemo.postman_collection.json -e .\CSVDEMOENV.postman_environment.json --iteration-data .\mydata.csv

This postman script will read in the csv file and loop through each csv row using the each row column data. This is awesome because you can run postman data driven from the command line. Youā€™ll get the results like this:

1 Like

Excellent write up! Thank you for this :smile: