PostMan 6.0.10 here. I'm trying to understand how to write test scripts a little better, and after reading their otherwise superb documentation I still have some confusion surrounding how to query & examine the JSON response coming back from requests.
Specifically, given the following snippet of JavaScript:
pm.test("Verify the contents of the response payload are correct", function () {
// ???
});
I need to be able to query the response JSON and:
- Determine if the response is a single JSON object or an array of JSON objects
- If its an array, determine the size (# of elements in the array)
- Else if its a single object, I need to be able to query that object for specific fields (say, a field called "
fizzbuzz
") and obtain the values and JSON types (string, number, bool, null) of those fields
Scenario #1: JSON response is an array
Example:
[
{
"fizz": "buzz",
"foo": 53
},
{
"fizz": "bozz",
"foo": 291
}
]
Scenario #2: JSON response is a single object
Example:
{
"fizz": "buzz",
"foo": 293
}
Any ideas how this JSON inspection of the response payloads can be performed?
pm.expect()
syntax and chai assertion. Also check out other questions on here for hints. It would be wrong of someone to write the code for you as you wouldn’t really be learning it for yourself. – Hunyadipm.response.to.have.jsonBody()
inside mypm.test
closure, but unless I'm missing something big in the docs, it doesn't look like I can actually inspect the output ofjsonBody()
. – Chorizopm.response.json()
depending what’s returned and what you what to assert against you can do that in anexpect
function.pm.expect(pm.response.json().whatever).to.equal('some_value')
– Hunyadi...json().whatever
and'some_value'
would actually look like in both my scenarios? Thanks again! – Chorizo