You can use the Newman command options
in here
-d, --iteration-data [file] : Specify a data file to use, either JSON or CSV.
#1 Save as data.json
this data, I suppose your data is this object of array format.
[
{
"num_of_steps": "1",
"testcases": [
{
"comment": "Login",
"test case": "1",
"data": {
"version": "2.1"
},
"Input": {
"name": "panda",
"password": "apple123"
}
}
]
},
{
"num_of_steps": "2",
"testcases": [
{
"comment": "Login",
"test case": "1",
"data": {
"version": "2.1"
},
"Input": {
"name": "monkey",
"password": "strong_password1234"
}
}
]
}
]
#2 Set global variable
To assign in Pre-request Script
tab of Postman
pm.globals.set("jsonBody", JSON.stringify(pm.iterationData.toObject()));
var jsonData = JSON.parse(pm.globals.get("jsonBody"))
var input = jsonData["testcases"][0]["Input"];
pm.globals.set("username", input?.name);
pm.globals.set("password", input?.password);
Will set Global Variable the username
and password
after POST call.

#3 Access variable in input body
In input Body of POST call
{
"username": "{{username}}",
"password": "{{password}}"
}

#4 Setup test in Tests
tab of Postman
var jsonData = JSON.parse(responseBody);
var username = jsonData["username"]
var password = jsonData["password"]
pm.test('Check username = ' + JSON.stringify(username), function () {
pm.expect(username).to.equal(pm.globals.get("username"));
});
pm.test('Check password = ' + JSON.stringify(password), function () {
pm.expect(password).to.equal(pm.globals.get("password"));
});

#5 Export collection


#6 Demo server by Flask of Python
It will play-back from POST of input body to output body
Save as the server.py
filename.
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/user", methods=["POST"])
def post_test():
content_type = request.headers.get('Content-Type')
if (content_type == 'application/json'):
json = request.get_json()
return json, 201
else:
return 'Content-Type not supported!'
if __name__ == "__main__":
app.run(debug=True)
#7 Install flask and run server
pip install flask
python server.py

#8 Run newman
with data.json
and the collection file.
newman run ./demo.postman_collection.json --iteration-data ./data.json
You can see the user and password back to output body.
Those values come from your JSON object array file.
Result Testing
