6

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?

5
  • 1
    What have you tried so far? For research should have taken you to the docs around tests where you can use the 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. Commented Mar 31, 2018 at 14:48
  • Thanks @DannyDainton (+1) I did see that I could nest something like pm.response.to.have.jsonBody() inside my pm.test closure, but unless I'm missing something big in the docs, it doesn't look like I can actually inspect the output of jsonBody().
    – smeeb
    Commented Mar 31, 2018 at 16:43
  • 1
    The whole response body will be pm.response.json() depending what’s returned and what you what to assert against you can do that in an expect function. pm.expect(pm.response.json().whatever).to.equal('some_value') Commented Mar 31, 2018 at 17:26
  • 1
    Add an example of your response body to the original question and it’ll be easier to advise to what to write. Without this, the response could literally be any valid JSON, no one would know what to tell you. Commented Mar 31, 2018 at 17:29
  • Thanks again @DannyDainton (+1 for both). I've included some sample JSON scenarios (both in the case of an array response as well as a single object response) to the original question. Can you help me figure out what the ...json().whatever and 'some_value' would actually look like in both my scenarios? Thanks again!
    – smeeb
    Commented Mar 31, 2018 at 17:37

2 Answers 2

10

This is basic but should work to get the dynamics:

pm.test("Verify payload of example one",  () => {
    pm.expect(pm.response.json()[0].fizz).to.equal('buzz')
    pm.expect(pm.response.json()[0].foo).to.equal(53)
    pm.expect(pm.response.json()[1].fizz).to.equal('bozz')
    pm.expect(pm.response.json()[1].foo).to.equal(291)
});

pm.test("Verify payload of example two",  () => {
    pm.expect(pm.response.json().fizz).to.equal('buzz')
    pm.expect(pm.response.json().foo).to.equal(293)
});

Might be worth researching some basic JavaScript and how to parse JSON objects.

-1

//You can do something like this

 if (responseCode.code === 200) {
  var jsonDataArray;
  var jsonArray= JSON.parse(responseBody);
 var found=false;

       for (var i = 0;i<jsonArray.length;i++){`enter code here`

     jsonDataArray = jsonArray[i];
     for (var j = 0; j<jsonDataArray.length && found === false;j++){
         if (jsonDataArray[j].fizz === "buzz" && jsonDataArray[j].foo === 53)

Not the answer you're looking for? Browse other questions tagged or ask your own question.