# Is it possible to pass a variable in a JsonPath?

**URL:** <https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [October 21, 2020, 3:22am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950 "2020-10-21T03:22:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpnistal19](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@mpnistal19](https://community.postman.com/u/mpnistal19)\
**Post date:** [October 21, 2020, 3:22am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/1 "2020-10-21T03:22:48Z")

</div>

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](https://community.postman.com/t/about-the-help-category/38) before creating a new topic.**

---

<div class="post-metadata">

**Author:** ![tounayang](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@tounayang](https://community.postman.com/u/tounayang)\
**Post date:** [February 4, 2022, 11:56pm UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/2 "2022-02-04T23:56:50Z")

</div>

Has anyone found a solution to this?

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [February 5, 2022, 12:24am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/3 "2022-02-05T00:24:06Z")

</div>

Here is a [code sample](https://www.postman.com/postman/workspace/postman-answers/request/9215231-1a78f558-d2f8-4cca-8ab6-c8f72252e111) of how to update a path variable dynamically.

---

<div class="post-metadata">

**Author:** ![tounayang](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@tounayang](https://community.postman.com/u/tounayang)\
**Post date:** [February 5, 2022, 1:20am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/4 "2022-02-05T01:20:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tounayang](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@tounayang](https://community.postman.com/u/tounayang)\
**Post date:** [February 5, 2022, 1:22am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/5 "2022-02-05T01:22:25Z")

</div>

I used the eval(JsonData.cap) but the eval() doesn’t work

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [February 7, 2022, 5:47pm UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/6 "2022-02-07T17:47:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tounayang](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@tounayang](https://community.postman.com/u/tounayang)\
**Post date:** [February 8, 2022, 12:56am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/7 "2022-02-08T00:56:34Z")

</div>

So here’s the example using this endpoint: [https://datausa.io/api/data?drilldowns=Nation&measures=Population](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  
});

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [February 8, 2022, 6:51pm UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/8 "2022-02-08T18:51:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tounayang](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@tounayang](https://community.postman.com/u/tounayang)\
**Post date:** [February 9, 2022, 1:55am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/9 "2022-02-09T01:55:35Z")

</div>

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://datausa.io/api/data?drilldowns=Nation&measures=Population,data%5B0%5D.Population,328239523)

[https://mykitchten.com/food,type.food[0].sauce,mariana](https://mykitchten.com/food,type.food%5B0%5D.sauce,mariana)

[https://datausa.io/api/data?drilldowns=Nation&measures=Nation,data[0].Nation,United](https://datausa.io/api/data?drilldowns=Nation&measures=Nation,data%5B0%5D.Nation,United) States

---

<div class="post-metadata">

**Author:** ![tounayang](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@tounayang](https://community.postman.com/u/tounayang)\
**Post date:** [February 9, 2022, 7:52am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/10 "2022-02-09T07:52:32Z")

</div>

Kevin, so I am have a csv file: exampledata.csv  
url,jsonpath,expectedvalue  
[http://abc.com/car,model[0].type,toyota](http://abc.com/car,model%5B0%5D.type,toyota)  
[http://xyz.com/fruit,type.fruit,banana](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.

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [February 9, 2022, 7:10pm UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/11 "2022-02-09T19:10:33Z")

</div>

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:

> **[Importing data files](https://learning.postman.com/docs/running-collections/working-with-data-files/)**

> **[Postman JavaScript reference](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-data-variables-in-scripts)**

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

---

<div class="post-metadata">

**Author:** ![tounayang](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@tounayang](https://community.postman.com/u/tounayang)\
**Post date:** [February 10, 2022, 12:23am UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/12 "2022-02-10T00:23:35Z")

</div>

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](https://us1.discourse-cdn.com/getpostman/original/2X/c/cc6f60ed45587f1398eccdba33dc1c061fc56008.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://datausa.io/api/data?drilldowns=Nation&measures=Population,data%5B0%5D.Population,328239523)  
[https://swapi.dev/api/people/1/,name,Luke](https://swapi.dev/api/people/1/,name,Luke) Skywalker  
[https://jsonplaceholder.typicode.com/todos/1,title,delectus](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:

 ![image.png](https://us1.discourse-cdn.com/getpostman/original/2X/c/c22c503c24db632de2d5048252bfb05104447cda.png)

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [February 10, 2022, 6:08pm UTC](https://community.postman.com/t/is-it-possible-to-pass-a-variable-in-a-jsonpath/16950/13 "2022-02-10T18:08:38Z")

</div>

Excellent write up! Thank you for this 😄
