CSV is great for flat data — simple key-value pairs. But what if your test data has nested objects? Arrays? Complex structures? CSV cannot handle that. JSON can.
The JSON data file must be an array of objects. Each object is one iteration. The keys become your data variables.
[
{
"testCase": "Valid order with single item",
"customerName": "Rahul Sharma",
"email": "rahul@example.com",
"product": "Laptop",
"quantity": 1,
"expectedStatus": 201,
"expectedMessage": "Order created successfully"
},
{
"testCase": "Valid order with multiple items",
"customerName": "Priya Patel",
"email": "priya@example.com",
"product": "Phone",
"quantity": 3,
"expectedStatus": 201,
"expectedMessage": "Order created successfully"
},
{
"testCase": "Invalid - zero quantity",
"customerName": "Test User",
"email": "test@example.com",
"product": "Tablet",
"quantity": 0,
"expectedStatus": 400,
"expectedMessage": "Quantity must be at least 1"
},
{
"testCase": "Invalid - missing email",
"customerName": "No Email User",
"email": "",
"product": "Charger",
"quantity": 2,
"expectedStatus": 400,
"expectedMessage": "Email is required"
}
]POST {{baseUrl}}/orders
Headers:
Content-Type: application/json
Authorization: Bearer {{authToken}}
Body (raw JSON):
{
"customer": "{{customerName}}",
"email": "{{email}}",
"items": [
{
"product": "{{product}}",
"quantity": {{quantity}}
}
]
}// Post-response script
const testCase = pm.iterationData.get("testCase");
const expectedStatus = pm.iterationData.get("expectedStatus");
const expectedMessage = pm.iterationData.get("expectedMessage");
pm.test(`${testCase}: Status ${expectedStatus}`, function () {
pm.response.to.have.status(expectedStatus);
});
pm.test(`${testCase}: Correct message`, function () {
const response = pm.response.json();
pm.expect(response.message).to.include(expectedMessage);
});
// Log iteration details to console
console.log(`Iteration: ${testCase} | Status: ${pm.response.code}`);Need to pass nested objects? You cannot do {{nestedObject}} directly in the body — Postman treats data variables as strings. Instead, build the body in a pre-request script.
// JSON data file has complex structure:
// [{ "address": { "street": "MG Road", "city": "Bangalore" }, "items": [1, 2, 3] }]
// Pre-request script: build body from iteration data
const address = pm.iterationData.get("address"); // Returns the object
const items = pm.iterationData.get("items"); // Returns the array
const requestBody = {
customer: pm.iterationData.get("customerName"),
shippingAddress: address,
itemIds: items
};
// Set as environment variable, use {{requestBody}} in body
pm.environment.set("requestBody", JSON.stringify(requestBody));When using JSON data files with numbers, be careful. In CSV, everything is a string — you must parseInt(). In JSON, numbers stay as numbers. But when used in {{}} syntax in the body, they become strings. If the API expects a number, use raw body mode and build the JSON in a pre-request script.
Always add a "testCase" field to your JSON data file — a human-readable description of what each iteration tests. It makes the Collection Runner results readable. Instead of seeing "Iteration 3 failed," you see "Invalid - zero quantity: Failed."
Key Point: Use JSON data files when test data has nested objects, arrays, or varying structures. CSV for flat data, JSON for complex data.