Converting Unix timestamps within Each

I’m using an html template within the Tests tab to output my json data, however, some of the data is in timestamp form. Namely, the date_created, first_seen, & last_seen are all being returned in timestamp form. Is there a way to do the calculation to a more standard date/time format from within the var template ? I’ve tried several ways to convert the timestamp, but none work within the template variable or cause the call to fail. Is it even possible to do within that template? Or how would you do it outside the template and then pull it back into the html? Because of the each loop I’m just not sure how to do this.
The other thing to keep in mind is the number of number of results can vary, so it’s not a fixed number of results I can hard code.


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;}
        .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>
    
            Dom: {{response.result.dom}}</br>
            Rep: {{response.result.rep}}</br>
            Reg: {{response.result.reg}}</br>
            Date Created: {{response.result.date_created}}</br>
            First Seen: {{response.result.first_seen}}</br>
            Last Seen: {{response.result.last_seen}}</br>
    <table class="tftable" border="1">
        <tr>
            <th>Name</th>
            <th>Rep</th>
            <th>First Seen</th>
            <th>Last Seen</th>
        </tr>
        <tr>

        </tr>
        {{#each response.result.ns}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{host}}</td>
                <td>{{rep}}</td>
                <td>{{first_seen}}</td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
               {{#each response.result.send}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{id}}</td>
                <td></td>
                <td></td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
    </table>
`;

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

The JSON response is like:

{
"code": 200,
    "result": {
        "dom": "abc",
        "rep": "green",
        "reg": "Going",
        "date_created": 1311292800,
        "first_seen": 1339185300,
        "last_seen": 1661781780,
        "ns": [
            {
                "host": "aws",
                "first_seen": 1517597640,
                "last_seen": 1661781780,
                "rep": "green"
            }
      ]
    }
}

Can you post an example of your response?

I’m not sure you can call a function to change the date with your variable, as it will probably just save it as text as part of the overall string. (But I’ll have a go).

What is the current format of the date? And what does it need to look like?

Apologies on that oversight. I’ve updated the post to add a sample json response with the format of the timestamps they’re passing back.

You need to parse the response first to a JavaScript object which you can then update before using in the visualizer. The following will update the initial elements but not the ones in the “ns” array. You will probably need to extend the code to loop though the array and create a new array to then use in the visualizer. I can’t think of any other way to do this. This is more of a JavaScript problem than Postman. So if you know a JavaScript developer, I would lean on them :slight_smile:

json = 
    {
    "code": 200,
        "result": {
            "dom": "abc",
            "rep": "green",
            "reg": "Going",
            "date_created": 1311292800,
            "first_seen": 1339185300,
            "last_seen": 1661781780,
            "ns": [
                {
                    "host": "aws",
                    "first_seen": 1517597640,
                    "last_seen": 1661781780,
                    "rep": "green"
                }
        ]
        }
    }

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;}
        .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>
    
            Dom: {{response.result.dom}}</br>
            Rep: {{response.result.rep}}</br>
            Reg: {{response.result.reg}}</br>
            Date Created: {{response.result.date_created}}</br>
            First Seen: {{response.result.first_seen}}</br>
            Last Seen: {{response.result.last_seen}}</br>
    <table class="tftable" border="1">
        <tr>
            <th>Name</th>
            <th>Rep</th>
            <th>First Seen</th>
            <th>Last Seen</th>
        </tr>
        <tr>

        </tr>
        {{#each response.result.ns}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{host}}</td>
                <td>{{rep}}</td>
                <td>{{first_seen}}</td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
               {{#each response.result.send}}
            <tr id=row_{{@key}} onClick="handleClick(this.id)">
                <td id={{@key}}>{{id}}</td>
                <td></td>
                <td></td>
                <td>{{last_seen}}</td>
            </tr>
        {{/each}}
    </table>
`;

var date_created = (new Date(json.result.date_created * 1000)).toISOString().slice(0, 10);
var first_seen = (new Date(json.result.first_seen * 1000)).toISOString().slice(0, 10);
var last_seen = (new Date(json.result.last_seen * 1000)).toISOString().slice(0, 10);

json.result.date_created = date_created;
json.result.first_seen = first_seen;
json.result.last_seen = last_seen;

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