Status code says "it worked." But the body tells you WHAT it returned. A 200 with wrong data is worse than a 500 — because at least a 500 screams "something broke." A 200 with bad data silently corrupts your frontend. That's the sneaky bug that takes 3 days to find.
First step — always parse the body. pm.response.json() converts the raw response into a JavaScript object you can inspect.
// Step 1: Parse the response body
const response = pm.response.json();
// Now you can access any field
console.log(response.id); // 1
console.log(response.title); // "sunt aut facere..."
console.log(response.userId); // 1const response = pm.response.json();
// Exact value match
pm.test("User ID is 1", function () {
pm.expect(response.userId).to.eql(1);
});
// String contains substring
pm.test("Title contains expected text", function () {
pm.expect(response.title).to.include("sunt aut");
});
// Field is not null or undefined
pm.test("Body is not empty", function () {
pm.expect(response.body).to.not.be.null;
pm.expect(response.body).to.not.be.undefined;
});
// Boolean check
pm.test("User is active", function () {
pm.expect(response.isActive).to.be.true;
});Value checks tell you "is the data correct?" Type checks tell you "is the data shape correct?" Both matter. If your frontend expects a number but gets a string "42" — it breaks.
const response = pm.response.json();
pm.test("ID is a number", function () {
pm.expect(response.id).to.be.a("number");
});
pm.test("Title is a string", function () {
pm.expect(response.title).to.be.a("string");
});
pm.test("Tags is an array", function () {
pm.expect(response.tags).to.be.an("array");
});
pm.test("Address is an object", function () {
pm.expect(response.address).to.be.an("object");
});Real APIs don't return flat objects. They return nested structures — user.address.city, order.items[0].price. You need to navigate deep into the response.
// Response from GET /users/1 on JSONPlaceholder:
// {
// "id": 1,
// "name": "Leanne Graham",
// "address": {
// "street": "Kulas Light",
// "city": "Gwenborough",
// "geo": { "lat": "-37.3159", "lng": "81.1496" }
// },
// "company": { "name": "Romaguera-Crona" }
// }
const user = pm.response.json();
pm.test("City is Gwenborough", function () {
pm.expect(user.address.city).to.eql("Gwenborough");
});
pm.test("Geo coordinates exist", function () {
pm.expect(user.address.geo).to.have.property("lat");
pm.expect(user.address.geo).to.have.property("lng");
});
pm.test("Company name is a string", function () {
pm.expect(user.company.name).to.be.a("string");
});// GET /posts returns an array of 100 posts
const posts = pm.response.json();
pm.test("Response is an array", function () {
pm.expect(posts).to.be.an("array");
});
pm.test("Array has 100 items", function () {
pm.expect(posts).to.have.lengthOf(100);
});
pm.test("First post has correct ID", function () {
pm.expect(posts[0].id).to.eql(1);
});
pm.test("Every post has a title", function () {
posts.forEach(function (post) {
pm.expect(post).to.have.property("title");
});
});Be careful with pm.response.json() — if the response is not valid JSON (e.g., HTML error page, empty body, 204 response), it will throw an error and ALL your tests will fail. Add a status code check first, then parse the body.
const response = pm.response.json();
// Check all required keys exist
pm.test("Response has all required fields", function () {
pm.expect(response).to.have.all.keys(
"userId", "id", "title", "body"
);
});
// Check at least some keys exist (partial check)
pm.test("Response has id and title at minimum", function () {
pm.expect(response).to.include.keys("id", "title");
});Q: How do you validate nested JSON fields in Postman?
A: I parse the response using pm.response.json(), then chain dot notation to access nested fields. For example, pm.expect(response.address.city).to.eql("Mumbai"). For arrays, I use index access like response.items[0].price. I also use .to.have.property() to check if a nested key exists before asserting its value — this gives a cleaner error message when the field is missing entirely.
Key Point: Parse with pm.response.json(), then assert values, types, and structure. Always check nested fields — real APIs are never flat.