Is it possible to pass a variable in a JsonPath?


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
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

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:
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 =[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,data[0].Population,328239523,[0].sauce,mariana,data[0].Nation,United States

1 Like

Kevin, so I am have a csv file: exampledata.csv

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)

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:

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

URL,JSONPATH,EXPECTEDVALUE,data[0].Population,328239523,name,Luke Skywalker,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: