Visualizer: Getting a specific Nested Object. Same name, but different values

Please community! Grace me with your wealth of knowledge. I’ve got hundreds of laptops I’m checking the warranty on through Dell’s API. The response returned looks like the one below. The visualize doesn’t give me the endDate either. The response is only on one machine for now, but the API lets me check up to 100 machines at once. The script for the visualizer is below it. I’m trying to build the visualize to only pull the “productLineDescription” “serviceTag” and a specific “endDate”. The problem for me there is that there are 3 different “endDates” listed under “entitlements”. I only want it to pull the 3rd one. I’d really like to get it to work so I can run 100 at a time.

[
{
“id”: 123456789,
“serviceTag”: “POSLPTP”,
“productLineDescription”: “LATITUDE 5590”,
“entitlements”: [
{
“itemNumber”: “997-8328”,
“startDate”: “2018-08-28T05:00:00Z”,
“endDate”: “2019-08-29T04:59:59.999Z”
“serviceLevelDescription”: “Onsite Service After Remote Diagnosis (Consumer
Customer)/ Next Business Day Onsite After Remote Diagnosis (for business Customer)”,
“serviceLevelGroup”: 5
},
{
“itemNumber”: “984-1952”,
“startDate”: “2018-08-28T05:00:00Z”,
“endDate”: “2021-08-28T04:59:59.999Z”,
“serviceLevelDescription”: “Keep Your Hard Drive/Keep Your Hard Drive for Enterprise
Service”,
“serviceLevelGroup”: 11
},
{
“itemNumber”: “997-8332”,
“startDate”: “2019-08-29T05:00:00Z”,
“endDate”: “2021-08-29T04:59:59.999Z”,
“serviceLevelDescription”: “Onsite Service After Remote Diagnosis (Consumer
Customer)/ Next Business Day Onsite After Remote Diagnosis (for business Customer)”,
“serviceLevelGroup”: 5
}
]
}
]

My Test Script in its original form. I’ve tried dozens of ideas, so I reset it back to the beginning.

var template =`

.tftable {font-size:14px;color:#333333;width:100%;border-width: 1px;border-color: #87ceeb;border-collapse: collapse;}
.tftable th {font-size:18px;background-color:#87ceeb;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;text-align:left;}
.tftable tr {background-color:#ffffff;}
.tftable td {font-size:14px;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;}
.tftable tr:hover {background-color:#e0ffff;}
.tftable

<table class="tftable" border="1">
    <tr>
        <th>Service Tag</th>
        <th>Product ID</th>
        <th>End Date</th>
    </tr>

    {{#each response}}
        <tr>
            <td>{{serviceTag}}</td>
            <td>{{productLineDescription}}</td>
            <td>{{endDate]}}</td>
        </tr>
    {{/each}}
</table>

`;

pm.visualizer.set(template, {
response: pm.response.json()
});

Bump! I know there are some geniuses out there that can help.

Is it always the third result? Or can some devices have more or less entitlements?

If its always the 3rd object in the entitlement array, then the following should work.

{{entitlements.[2].endDate}}

The visualizer supports the Handlebars JS library.

Handlebars (handlebarsjs.com)

The Handlebars treats the array slightly differently. I had to search on how to target the array because the normal ‘entitlements[2]’ was throwing errors.

let response = pm.response.json().data
// you can drop the .data (this is just how Postman echo returns the response)

console.log(response);

var template = `

    <style type="text/css">
        .tftable {font-size:14px;color:#333333;width:100%;border-width: 1px;border-color: #87ceeb;border-collapse: collapse;}
        .tftable th {font-size:18px;background-color:#87ceeb;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;text-align:left;position:sticky;top:0;}
        .tftable tr {background-color:#ffffff;}
        .tftable td {font-size:14px;border-width: 1px;padding: 8px;border-style: solid;border-color: #87ceeb;}
        .tftable tr:hover {background-color:#e0ffff;}
    </style>
    
    <table class="tftable" border="1">

        <tr>
            <th>id</th>
            <th>serviceTag</th>
            <th>endDate</th>
        </tr>

        {{#each response}}
            <tr>
                <td>{{id}}</td>
                <td>{{serviceTag}}</td>
                <td>{{entitlements.[2].endDate}}</td>
            </tr>
        {{/each}}

    </table>

`;

console.log(response);

pm.visualizer.set(template, {
    response: response
});
1 Like

Ok I’ll give it a try! Thanks for the feedback. I’ll let you know.

You have just made my day! Works exactly like I want. Thanks for the info on HandlBars JS library too.

1 Like