Data-Driven Testing in Postman with External CSV and JSON Files
Run one Postman request across many inputs using Collection Runner and pm.iterationData, with limits and common mistakes.
17 Aug 2025, 07:46 UTC

The problem and the useful answer
If you are testing the same endpoint with dozens of different inputs, duplicating the request for every case creates a collection that is hard to maintain. The useful takeaway is to keep one request template and feed it rows from an external CSV or JSON file through Postman Collection Runner. Each row runs as one iteration, and your scripts read the current row with pm.iterationData.get('columnName').
How the runner uses a data file
Postman Collection Runner can iterate a collection over rows in a CSV file or a JSON array. Before the run, you select the data file. During the run, Postman assigns one row to each iteration. You can reference columns directly in a request using double-brace syntax such as {{userId}}, or read them in scripts with pm.iterationData.get('userId').
Data variables exist only for the current iteration. Collection and environment variables can be overridden during an iteration, while global variables persist across runs unless cleared. Values set with pm.variables.set inside a script are local to that script execution and do not automatically carry into the next iteration.
Worked example: CSV-driven user lookup
Create a single GET request with this URL, replacing {{apiBase}} with your API host:
{{apiBase}}/users/{{userId}}
Add an environment variable named apiBase. Do not put API keys or PII in the data file; store secrets in the Postman Vault or an environment variable.
In the Pre-request script, log the current row and optionally store it in a local variable:
const userId = pm.iterationData.get('userId');
console.log('Iteration for userId: ' + userId);
pm.variables.set('currentUserId', userId);
In the Tests tab, assert the status and verify that the response matches the row:
pm.test('Response status is 200', function () {
pm.response.to.have.status(200);
});
pm.test('Returned user matches data file', function () {
const responseJson = pm.response.json();
const expectedId = Number(pm.iterationData.get('userId'));
pm.expect(responseJson.id).to.eql(expectedId);
});
Save a CSV file named data.csv with a header row:
userId,expectedName
101,Leanne Graham
102,Ervin Howell
103,Clementine Bauch
Or save a JSON file named data.json as an array of objects:
[
{ "userId": "101", "expectedName": "Leanne Graham" },
{ "userId": "102", "expectedName": "Ervin Howell" },
{ "userId": "103", "expectedName": "Clementine Bauch" }
]
Open the Collection Runner in Postman, select your collection and environment, choose the data file under Data, and start the run. The runner executes the request once per row.
Running the same file in Newman
Newman is the Postman CLI and runs the same collections and data files in CI with identical iteration semantics. Run this in a terminal where Node.js and Newman are installed:
newman run ./my-collection.json -d ./data.csv -e ./my-environment.json
Required permissions: read access to the collection JSON, environment JSON, and data file. A meaningful risk is that if data.csv contains credentials, those values may appear in runner output or CI logs. Do not commit files with secrets to version control.
CSV versus JSON data files
| Format | Requirements | Notes |
|---|---|---|
| CSV | First row must be a header row | Every cell is treated as a string; coerce numbers with Number() in scripts. |
| JSON | Must be a valid JSON array of objects | Preserves numbers, booleans, and nested objects; better for complex payloads. |
Limits and common mistakes
- Malformed files: Invalid CSV or JSON causes the runner to skip or fail iterations. Validate the file before adding it to the runner.
- No built-in row limit: Large data files can slow the runner. Subset the file manually if you only need a sample run.
- Secrets in data files: Never hard-code API keys or PII in CSV or JSON data. Use environment variables or the Postman Vault, and exclude secret files from version control.
- Variable scope:
pm.variables.setdoes not persist across iterations. Usepm.environment.setorpm.collectionVariables.setonly if you intentionally need cross-iteration state. - String coercion: CSV values are strings. Comparing
pm.iterationData.get('id')directly to a numeric response field can fail unless you convert it withNumber().
Check that it worked
- Open the runner results and confirm the iteration count equals the number of data rows.
- Watch the Postman console;
pm.iterationData.get('userId')should change on every iteration. - Run the same collection and file with Newman and compare the iteration count and assertion results to the desktop runner.
- Export the collection JSON and confirm it does not contain secret values from your data file.
If a pre-request or test script writes to environment or collection scope, that value survives the run. Reset it manually before rerunning if you need a clean baseline.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.