As a part of End to End API Manual and Automation Testing using Postman Series , in this post, we will see how can we read data from an external JSON file in Postman.
In previous post, we have learnt how to read data from a csv file in Postman. Many steps will be similar for JSON file as well . We will see step by step to read data from a json file in Postman here. I will strongly recommended you to go through previous post to understand need of reading data from an external file and example API.
Step 1:- Create a JSON file
We need to create a JSON file in specific format so that Postman can read it. It must be a JSON Array of JSON Objects. An example is as below:-
[ { "username": "admin", "password": "admin" } ]
We know a JSON is key-value pair. Key will be used as a variable name in Postman and values of key will be used by Postman to evaluate. We can use key as variable like any other variable in Postman. For ex:- {{keyName}}. At run time, value of key will be used.
So for the example API, we will create a JSON file. I will keep key names similar to fields in request body for better understanding and readability.
[ { "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "checkin": "2018-01-01", "checkout": "2019-01-01", "additionalneeds": "Breakfast" } ]
You can create a JSON file using any online editor. After creating json file, save it with .json extension.
Now all steps are similar to previous post. There is no change.
Step 2:- Parameterised request
Remember we can use json variables in URL, headers, request , scripts etc as well. Here we are using in request body.
Using variables from json file is same as any variable from Environment, global or collection i.e. as {{variableName}}.
Note:- If a variable value is a string type , put variable name in double quotes. If it is number type or boolean , put it without double quotes. See examples below:-
Step 3: – Run the API
If we are reading data from a json/csv, we can not run it as a normal request by clicking on “Send” button. If we click on Send button to run above request, it will give you error “400 – Bad Request” because it will not able to get values for the variables defined. We have not told Postman where to look for values yet.
So we need to use Collection Runner for this. Save above request in a collection and launch collection runner. You can find all concepts of Collection and Collection Runner on my blog here.
In Collection Runner, you will see a field named “Data” with “Select File”. Here you need to select json file which we created above. Click on “Select File” button and select the json file. You will see after selection, Data File type will automatically show as application/json. You can preview the file.
Now run the collection by clicking on “Run <Collection Name>” button.
You will see Run result of Collection. You can see json request takes values from json file.
You can import example collection from here:-
https://www.getpostman.com/collections/e7d3735a7de9ea07870e
#ThanksForReading
Did you know , Java programming questions are asked in almost all interviews ? Have you prepared that? Not yet. Get started here.
The main point is to remember json arrays stars with [ ] . struggled for half an hour.
just for reference for everone doing this