GET request with variable data stored in external JSON file causing issues

Hi All,

I would like to test my GET request with 4 iterations using data stored as 4 objects in external JSON file. As its JSON file I have stored it in key value pair as below:

[

   {
    "ABC":"AAA",
	"XYZ": "BBB"
   },
   {
    "DEF":"CCCC",
	"GHI": "DDD"
   }     		
 ]

Values are getting picked/stored as “AAA”, “BBB” “CCCC”,“DDD” in environment variable and not as AAA simply. Because of double quotes those are getting assigned as %22AAA%22 after encoded by Postman in GET URL. This is causing me to get response code as 404 and not 200. What is the right way to test GET API with data variation using external JSON file?

Hey @rohit2330

Are you also able to provide an example of the request and test script that you’re using, please?

Hi @dannydainton,

Below are URL and script details which I am trying to fetch data from JSON and store in the environment variable.

URL:

{{URL}}/test?param1=123&param2=001&param3=0&ABC={{ABC}}&XYZ={{XYZ}}


Script in Prerequisite tab-
pm.environment.set("ABC", JSON.stringify(data.ABC));
pm.environment.set("XYZ", JSON.stringify(data.XYZ));

I am trying test parameter ABC and XYZ with at least 4 iteration.

Thanks.

You can remove the pre-request script as the variables in the URL would be resolved to the values in the data file.

You mentioned 4 iterations, not sure what you mean there?

The example data file you mentioned would give you 2 iterations but only the first object would resolve the variables in your URL.

Using this and changing the iteration value to 4 would use these in the requests:

[
   {
    "ABC": "AAA",
	"XYZ": "BBB"
   }		
]

Or adding this and it would auto-select the iteration count:

[
   {
    "ABC": "AAA",
	"XYZ": "BBB"
   },
   {
    "ABC": "AAA",
	"XYZ": "BBB"
   },
   {
    "ABC": "AAA",
	"XYZ": "BBB"
   },
   {
    "ABC": "AAA",
	"XYZ": "BBB"
   }
]

You need to ensure that if you have made changes to the request in the builder, that you save the changes in the request tab before running the collection.

I mentioned 4 iteration but in example given only 2 my bad.

Yes I have 4 objects in my JSON and i am going to add more data to these objects to use data variation for those requests. In short i wish to use 1 single file which will contain data sets for multiple request from collection.

I am using pm.environment.set(“ABC”, JSON.stringify(data.ABC)); to notify which data need to be picked from file as file will hold data for other requests too.

Can you suggest me best way to test API collection with multiple iterations(Each iteration will hold different data) for all requests in the collection having data stored in single external JSON file.

So for e.g. I have Collection as CCC which includes Request (POST),Request 2 (GET),Request 3(GET) and data for these requests is need to provide from external JSON

If you do need to access certain part of the data in the file, I would recommend using pm.iterationData.get() and that could be used like this:

pm.environment.set("ABC", pm.iterationData.get("ABC"))

I mentioned removing that script because if you only have {{ABC}} in the request, the value would have been resolve from the script without the need to add the pre-request setting.

In terms of good practices - @vdespa’s tutorials are always a good start:

and our learning center too

https://learning.postman.com/docs/postman/collection-runs/working-with-data-files/

2 Likes

Thanks for this information and helping me, I can proceed with this now.