Separating part of response into separate strings

My question:
I’m able to use visualizer to separate known fields into columns, etc…but I need to break up a part of the response.

Details (like screenshots):
Example response line I’m looking to modify:
“details”: “{"config_id":24,"config_rev_id":26,"intel_id":1137,"service_id":"8c774976-3158-4692-bcd0-87a45e35dd36"}”,

Ideally I’d call out the "string"\ as a header and the following :## as a value.

Don’t think you need to do that.

If you just remove the back slashes, then that field will valid Json, that you should be able to loop through.

I’m not actually sure how Postman will see those back slashes though, as they are not escaped, so might be interpreted as escape characters and completely ignored.

let str = "{\"config_id\":24,\"config_rev_id\":26\}";

json = JSON.parse(str);

console.log(json);

image

Outside of visualiser it works without the replace command. Not sure if the foreach handlebars in the visualiser can work with this as a string or whether you need to do a similar operation and parse it somehow.

If you can post a sample response using the preformatted text option, I can take a look at what it looks like in visualiser and whether I can get the loop to work properly.

I’m assuming this has more than one record, so can you please include 2-3 records in the example response.

I might not be able to look at this until Monday. Works Xmas party a bit later.

1 Like

What we might be able to do is to convert the element into an array, and then write it back to the response.

If you parse the response using LET or VAR (instead of CONST), you should be able to update any element in the response before consuming it in the visualiser.

let initialStr = "{\"config_id\":24,\"config_rev_id\":26\}";

let updatedString = "["+initialStr+"]";

console.log(updatedString); // string

let array = JSON.parse(updatedString);

console.log(array);

image

Here’s the top four of the response JSON:

[
    {
        "id": 233565,
        "type": "removed",
        "guid": "3b6092ec-f83e-49a8-a587-1bd580c21a1d",
        "state": "unresolved",
        "priority": "high",
        "severity": "warn",
        "details": "{\"config_id\":24,\"config_rev_id\":26,\"intel_id\":1137,\"service_id\":\"8c774976-3158-4692-bcd0-87a45e35dd36\"}",
        "alertedAt": "2022-12-03T00:40:33.000Z",
        "createdAt": "2022-12-03T00:43:58.103Z",
        "updatedAt": "2022-12-03T00:43:58.103Z",
        "computerName": "removed",
        "computerIpAddress": "removed"
    },
    {
        "id": 233566,
        "type": "removed",
        "guid": "b2993ebd-84d4-401c-bfd4-81f6e74d1f51",
        "state": "unresolved",
        "priority": "high",
        "severity": "warn",
        "details": "{\"config_id\":12,\"config_rev_id\":7,\"intel_id\":683,\"service_id\":\"8c774976-3158-4692-bcd0-87a45e35dd36\"}",
        "alertedAt": "2022-12-03T03:21:54.000Z",
        "createdAt": "2022-12-03T03:23:23.060Z",
        "updatedAt": "2022-12-03T03:23:23.060Z",
        "computerName": "removed",
        "computerIpAddress": "removed"
    },
    {
        "id": 233567,
        "type": "removed",
        "guid": "fc1e19e0-3ebd-4ffc-9c16-250958babdf7",
        "state": "unresolved",
        "priority": "high",
        "severity": "warn",
        "details": "{\"config_id\":24,\"config_rev_id\":26,\"intel_id\":2728,\"service_id\":\"8c774976-3158-4692-bcd0-87a45e35dd36\"}",
        "alertedAt": "2022-12-03T08:38:00.000Z",
        "createdAt": "2022-12-03T08:38:29.149Z",
        "updatedAt": "2022-12-03T08:38:29.149Z",
        "computerName": "removed",
        "computerIpAddress": "removed"
    },
    {
        "id": 233568,
        "type": "removed",
        "guid": "968d704b-884f-4d6a-bcd0-468ee3221e32",
        "state": "unresolved",
        "priority": "high",
        "severity": "warn",
        "details": "{\"config_id\":21,\"config_rev_id\":27,\"intel_id\":1137,\"service_id\":\"8c774976-3158-4692-bcd0-87a45e35dd36\"}",
        "alertedAt": "2022-12-03T10:20:20.000Z",
        "createdAt": "2022-12-03T10:20:46.430Z",
        "updatedAt": "2022-12-03T10:20:46.430Z",
        "computerName": "removed",
        "computerIpAddress": "removed"```

This appears to work. (I changed the computer names).
I didn’t use all of the attributes, but hopefully you can see the logic.

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

// update details string in response to an array
for (let i = 0; i < response.length; i++) {
    let initialStr = response[i].details
    let updatedString = "[" + initialStr + "]";
    response[i].details = JSON.parse(updatedString);
}

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>guid</th>
            <th>config_id</th>
            <th>intel_id</th>
            <th>computerName</th>
        </tr>
        
        {{#each response}}

            <tr>

                <td>{{id}}</td>
                <td>{{guid}}</td>

                {{#each details}}
                    <td>{{config_id}}</td>
                    <td>{{intel_id}}</td>
                {{/each}}
                
                <td>{{computerName}}</td>

            </tr>

        {{/each}}

    </table>

`;

console.log(response);

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

1 Like

This looks perfect, however I get a Test Results error with:
"There was an error in evaluating the test script: TypeError: Cannot read properties of undefined (reading ‘length’) that I haven’t been able to slip in a solution for (successfully) yet.

Did you remove the .data from the first line?

I’m using Postman echo which just echo’s back what you send in the body.

However, it returns the information under a data object, so when I parse the response I include the data element and drop all of the other elements that Postman echo returns.

For example

image

I’ve tried it with both it removed and present. I checked if I’m possibly out-dated on Mac, but I’m running the latest v10.6.7

The error is telling you that the array is empty or is not an array (therefore it can’t return the length).

The Console log is your friend here.

let response = pm.response.json();
console.log(response);

What is being returned?

Can you please screenshot and share (redacted as appropriate).

Please expand the elements when you create the screenshot like the following example.

image

1 Like

It worked today for an unknown reason. I hadn’t closed Postman, so not sure what the change was. Thanks for your assistance AND patience, it’s been very valuable!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.