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?