jsonregexpostmanvisualize

How to match content in json response in Postman?


Been trying to figure how to get regex working in Postman, but can't seem to zone in on what I'm doing wrong.

Sample:

{
    "start": 0,
    "end": 8,
    "status": "SUCCESS",
    "total": 9,
    "spam_count": 0,
    "newsletter_count": 9,
    "dataprotection_count": 0,
    "imagecontrol_count": 0,
    "dlp_count": 0,
    "compliance_count": 0,
    "mail_list": [
        {
            "id": "e1bc1808526fb75588281510a004e8e1c170a1f54bc3c93942d247021bfd3094",
            "metadata": {
                "email_date_received": 1665160705707,
                "quarantine_info": {
                    "direction": "inbound",
                    "quarantine_type": "NEWSLETTER"
                },
                "email_is_viewed": false,
                "email_is_released": false,
                "quarantine_reason": "AS",
                "email_sender": "Stu Sjouwerman (ssjouwerman@knowbe4.com)",
                "service_type": "ess",
                "master_recipient": "demo@domain.co.uk",
                "user_id": 16813960,
                "email_envelope_sender": "1axbtdwbzef4jmdi2c7gwtint0y4ht9y9ycw9p@241394m.knowbe4.com",
                "email_subject": "[FREE Resource Kit] The Cybersecurity Awareness Month Kit for 2022 is Now Available",
                "email_size": 74004,
                "email_envelope_recipient": "demo@domain.co.uk"
            },
            "actions": {
                "view_subject": true,
                "delete_message": true,
                "preview_message": true,
                "release_message": true
            }
        }
]
}

With the example below, I can get the ID fine when checking for an exact match, email size is 74004. But I've been trying to check for partials, like if the subject has a word, example instead of checking the email size, to check if the subject contains the word "Resource", or even via regex matching.

Tried pm.expect(counter.metadata.email_subject).contains("Password"); and also pm.expect(counter.metadata.email_subject).to.match(/Password/); but always get an error.

responseJson = JSON.parse(responseBody);
  var a=[];
  var schID;
  var list = (responseJson.mail_list).length;

console.log(list);
   for (var i = 0; i < list; i++) 
    {
    var counter = responseJson.mail_list[i];

    //pm.expect(counter.metadata.email_subject).contains("Password");
    //pm.expect(counter.metadata.email_subject).to.include("Password");

    if (counter.metadata.email_subject == "74004"){
        schID=counter.id
        a.push(schID)
    }

    }
    a= JSON.stringify(a)

UPDATE 1:

Thanks @Onno, works great.


Solution

  • In order for Postman to run your code as a test, you have to wrap your function into a pm.test block, like so:

    pm.test("Put a testname here", function() {
        // your code goes here
    });
    

    and then you can match the JSON body by putting your regex as the argument to the .to.match() function, e.g. this should pass

    pm.test("Put a testname here", function() {
        const body = pm.response.json();
        pm.expect(body.mail_list[0].metadata.email_subject).to.match(/Awareness/);
    });