json 如何在Postman中可视化过滤后的响应?

xvw2m8pv  于 2022-11-19  发布在  Postman
关注(0)|答案(1)|浏览(218)

我可以得到一个精简的结果集,一个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
            }
        }
    ]
}

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

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可视化工具只关注过滤的数据,而不是整个。看了看如何过滤响应到一个新的更小的响应,但不能让它工作。
而不是得到3个结果

查看所显示的筛选值,匹配控制台输出

感谢@OnnoHow to match content in json response in Postman? And visualize中提供的帮助

exdqitrt

exdqitrt1#

您只能传递筛选数据变量,并在HTML中使用该变量键。
//下面的代码可以帮助您解决问题。

var responseToHtml = []; // please create this variable before for loop starts
//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);
            //adding object required to show in the visualizer.
            responseToHtml.push({"subject" : subject_in,"sender" : sender_in, "recipient" : recipient_in});
        }

//in visualization code you can pass responseToHtml variable.

//Visualizer
var template = `
    <table bgcolor="#FFFFFF">
        <tr>
            <th>Subject</th>
            <th>Sender</th>
            <th>Recipient</th>
        </tr>
        
        {{#each response}}
            <tr>
                <td>{{subject}}</td>
                <td>{{sender}}</td>
                <td>{{recipient}}</td>
            </tr>
        {{/each}}
    </table>
`;
 
// Set visualizer
pm.visualizer.set(template, {
    // Sending responseToHtml variable array as part of visualizer variables.
    response: responseToHtml

});

相关问题