我一直在试图弄清楚如何让正则表达式在Postman中工作,但似乎无法意识到我做错了什么.

示例:

{
    "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
            }
        }
]
}

通过下面的例子,我可以在判断精确匹配时获得ID,邮箱大小为74004.但我一直在try 判断分词,比如主题是否有单词、示例,而不是判断邮箱大小,判断主题是否包含单词"资源",甚至通过正则表达式匹配.

已try pm.expect(counter.metadata.email_subject).contains("Password");和pm.expect(counter.metadata.email_subject).to.match(/Password/);,但总是出现错误.

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,效果很好.

我能再问一个建议吗?

我现在可以得到一组精简的结果,一组ID,非常感谢.

我如何可视化返回的子集,而不是整个响应.

样本完整,3个邮件对象.

{
    "start": 0,
    "end": 2,
    "status": "SUCCESS",
    "total": 3,
    "spam_count": 0,
    "newsletter_count": 0,
    "dataprotection_count": 3,
    "imagecontrol_count": 0,
    "dlp_count": 0,
    "compliance_count": 3,
    "mail_list": [
        {
            "id": "aad7d101d57e29d955404fdb656fbf19f1c555b11058f20c0f706dd580518784",
            "metadata": {
                "email_date_received": 1665753452481,
                "quarantine_info": {
                    "rules": [
                        "Unicode test"
                    ],
                    "direction": "inbound",
                    "quarantine_type": "CI"
                },
                "email_is_viewed": false,
                "email_is_released": true,
                "quarantine_reason": "CC",
                "email_sender": "Rob Mont (rob_mont@domain.com)",
                "service_type": "ess",
                "master_recipient": "administrator@domain1.uk",
                "user_id": 98,
                "email_envelope_sender": "rob_mont@domain.com",
                "email_released_to": "recipient",
                "email_subject": "very bad naughty",
                "email_size": 6615,
                "email_envelope_recipient": "administrator@domain1.uk"
            },
            "actions": {
                "view_subject": true,
                "delete_message": true,
                "preview_message": true,
                "release_message": true
            }
        },
        {
            "id": "623cb796e5a61237b6e58b06b8934da5698df8fdfebe6c96c61b5da4adb19e1d",
            "metadata": {
                "email_date_received": 1665753296804,
                "quarantine_info": {
                    "rules": [
                        "Unicode test"
                    ],
                    "direction": "inbound",
                    "quarantine_type": "CI"
                },
                "email_is_viewed": false,
                "email_is_released": false,
                "quarantine_reason": "CC",
                "email_sender": "Rob Mont (rob_mont@domain.com)",
                "service_type": "ess",
                "master_recipient": "demo@domain1.uk",
                "user_id": 16813960,
                "email_envelope_sender": "rob_mont@domain.com",
                "email_subject": "test dlp_trigger_word",
                "email_size": 6686,
                "email_envelope_recipient": "demo@domain1.uk"
            },
            "actions": {
                "view_subject": true,
                "delete_message": true,
                "preview_message": true,
                "release_message": true
            }
        },
        {
            "id": "bd1d3899019aa3e45087811081b4d7c237d637385b9f3fd5cf3b57243594824c",
            "metadata": {
                "email_date_received": 1665753295105,
                "quarantine_info": {
                    "rules": [
                        "Unicode test"
                    ],
                    "direction": "inbound",
                    "quarantine_type": "CI"
                },
                "email_is_viewed": false,
                "email_is_released": false,
                "quarantine_reason": "CC",
                "email_sender": "Rob Mont (rob_mont@domain.com)",
                "service_type": "ess",
                "master_recipient": "administrator@domain1.uk",
                "user_id": 98,
                "email_envelope_sender": "rob_mont@domain.com",
                "email_subject": "test dlp_trigger_word",
                "email_size": 6690,
                "email_envelope_recipient": "administrator@domain1.uk"
            },
            "actions": {
                "view_subject": true,
                "delete_message": true,
                "preview_message": true,
                "release_message": true
            }
        }
    ]
}

新代码,根据当前 Select 只返回一个结果,这是可以的.

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];
        var subject_out = 0, type_out = 0;
        var subject_in, sender_in, recipient_in;

        //Checks the type of detection
        pm.test("Check Policy Name", function() {
            const body = pm.response.json();

                //In the match segment we can wtite our regex triggers content between the two / /
                if (pm.expect(body.mail_list[i].metadata.quarantine_info.rules).to.match(/Unicode test/)){
                    type_out = 1;        
                }else{
                    type_out = 0;
                }

            }
        );

        //Checks the subject line
        pm.test("Check Subject Trigger", function() {
            const body = pm.response.json();

                //In the match segment we can wtite our regex triggers content between the two / /
                if (pm.expect(body.mail_list[i].metadata.email_subject).to.match(/naughty/)){
                    
                    subject_in = body.mail_list[i].metadata.email_subject;
                    sender_in = body.mail_list[i].metadata.email_envelope_sender;
                    recipient_in = body.mail_list[i].metadata.email_envelope_recipient;
                    
                    subject_out = 1;
                }else{
                    subject_out = 0;
                }
            
            }
        );
        //If both type and subject are true, then return that email ID
        
        if (subject_out == 1 && type_out == 1){
            console.log("Subject: " + subject_in,"Sender: " + sender_in, "Recipient: " + recipient_in);
            schID=counter.id
            a.push(schID)
            
        }

    }

//Assigned the array with the returned IDs to the variable schID
a = JSON.stringify(a)
postman.setEnvironmentVariable("schID", a);

//Visualizer
var template = `
    <table bgcolor="#FFFFFF">
        <tr>
            <th>Subject</th>
            <th>Sender</th>
            <th>Recipient</th>
        </tr>
 
        {{#each response.mail_list}}
            <tr>
                <td>{{metadata.email_subject}}</td>
                <td>{{metadata.email_envelope_sender}}</td>
                <td>{{metadata.email_envelope_recipient}}</td>
            </tr>
        {{/each}}
    </table>
`;
 
// Set visualizer
pm.visualizer.set(template, {
    // Pass the response body parsed as JSON as `data`
    response: JSON.parse(responseBody)

});

Console result:
"Subject: very bad naughty" "Sender: rob_mont@domain.com" "Recipient: administrator@domain1.uk"

但在环顾四周后,我想不出如何让html可视化工具只关注过滤后的数据,而不是整个数据.查看了如何将响应过滤为新的较小的响应,但无法使其工作.

推荐答案

为了让Postman将您的代码作为测试运行,您必须将您的函数包装到一个pm.test块中,如下所示:

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

然后,您可以通过将您的正则表达式作为.to.match()函数的参数来匹配JSON主体,例如,这应该传递

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

Json相关问答推荐

如何在Vega中使标记的符号在鼠标指针悬停时可点击

无法使用Jolt变换在嵌套的JSON中提取值

如何从一个700MB的json文件中列出PowerShell中的所有密钥?

如何使用PowerShell从ExchangeOnline命令执行中获得JSON输出

json 字符串到 Postgres 14 中的表视图

如何编写 jolt 规范以将不同的对象转换为数组

jq - 将父键值提取为子元素旁边的逗号分隔值

添加到数组时出错:找不到Add的重载和参数计数:1

是否可以在有条件的情况下将 json 对象转换为 JOLT 中的数组?

Oracle Apex - 将 JSON 对象分配给变量以返回

jq:来自嵌套 JSON 的映射

在 postgresql 中将行转换为 json 对象

序列化特定类型时如何使 JSON.Net 序列化程序调用 ToString()?

如何使用 Newtonsoft.Json 反序列化 JSON 数组

.NET CORE 3 升级 CORS 和 Json(cycle) XMLHttpRequest 错误

我应该如何处理 JSON 中的 HATEOAS 链接和引用?

NSManagedObject 属性值的 NSNull 处理

JSON 和 BSON 哪个更轻量级?

将 JSON 模式转换为 python 类

Java HashMap 与 JSONObject