Un-escape JSON String from the response

Hi All,
Is there any setting in POSTMAN where I can get rid of an escape character backslash from the string response.
When I debug the response from oracle , it shows the expected response without backslash .
However, POSTMAN response shows escape character backslash .

Hi @gasogb!

So it looks like your “value” prop is a string which is why there are escape character backslashes being used (otherwise your quotation marks wouldn’t work!). Postman does not inherently alter the API response. It will display the response as it is received.

If you want to view this string as a JSON object in Postman, you can parse it using JavaScript in the “Tests” tab.

let response = pm.response.json(); // Parse the response
let value = JSON.parse(response.value); // Parse the 'value' field
console.log(value); // Log the result to the console

When you run this script, the value field will be logged to the Postman console as a JSON object, without the backslashes.

2 Likes

Thank you very much @kevinc-postman .
It works like a charm!
Your prompt response is much appreciated.

I have similar issue and I cant solve it.

My response looks like this

I need format response which is located in: response.datafieldData.g_Result

I tried everything, but im not able to get it.
Any help will be appreciated.

You will need to JSON.parse the g_Result which should then allow you access to the elements within.

Im doing it using like [kevinc-postman] wrote, but I getting difficulties to make it work.

TypeError: Cannot read properties of undefined (reading ‘g_Result’)

or

Couldn’t evaluate the test script:
SyntaxError: missing ) after argument list

Im getting this above

let responseNew = pm.response.json(); // Parse the response

let value = JSON.parse(responseNew.response.data[0]fieldData.g_Result); // Parse the 'value' field

console.log(value); // Log the result to the console

Think you are missing a dot.

responseNew.response.data[0]fieldData.g_Result

Should be


responseNew.response.data[0].fieldData.g_Result

If this doesn’t work, can you paste the example response as text instead of a screenshot, and I will help you to target those elements.

1 Like
{
    "response": {
        "scriptError.prerequest": "0",
        "dataInfo": {
            "database": "DB_v1",
            "layout": "L00_API_CONNECTOR",
            "table": "API_Connector",
            "totalRecordCount": 1,
            "foundCount": 1,
            "returnedCount": 1
        },
        "data": [
            {
                "fieldData": {
                    "g_Result": "{\"Locations\":[{\"addressLine1\":\"Egtasi 1 with ergameni\",\"addressLine2\":\"Site1\",\"city\":\"Peri\",\"country\":\"Εllll\",\"customerID\":\"4922\",\"latitude\":\"\",\"locationCode\":\"\",\"locationID\":\"49650\",\"locationName\":\"126\",\"locationNameAndCodeCombo\":\"-126\",\"locationTypeID\":\"004E-9FC0-C15068EBB3BE\",\"longitude\":\"\",\"numberOfEmployees\":0,\"postCode\":\"12133\",\"primaryContactID\":\"\",\"userID\":\"\"},{\"addressLine1\":\"address111\",\"addressLine2\":\"ΛΔωφ. ÎœÎ±ÏŽÎœÎżÏ‚ (17 χλΌ.)\",\"city\":\"NoNsame\",\"country\":\"ÎŹÏ‚\",\"customerID\":\"11501\",\"latitude\":\"\",\"locationCode\":\"\",\"locationID\":\"52912\",\"locationName\":\"ΘNoName\",\"locationNameAndCodeCombo\":\"-NoName\",\"locationTypeID\":\"-4D28-4C04-84AB-2FADDAC2A3C8\",\"longitude\":\"\",\"numberOfEmployees\":0,\"postCode\":\"15351\",\"primaryContactID\":\"\",\"userID\":\"\"}]}",
                    "g_userID": "19414",
                    "g_appSessionID": "F0E9-4B17-9E0E-9D436D40B04C",
                    "g_Date_30_days": "",
                    "g_Date_Minus_10_Days": "",
                    "g_visitStatusID": "",
                    "g_LocationSettingsID": "",
                    "g_locationID": "2",
                    "g_auditorType": "",
                    "g_auditType": ""
                },
                "portalData": {},
                "recordId": "1",
                "modId": "58192"
            }
        ]
    },
    "messages": [
        {
            "code": "0",
            "message": "OK"
        }
    ]
}
const response = pm.response.json();

let g_Result = JSON.parse(response.response.data[0].fieldData.g_Result);

console.log(g_Result);

Thanks, but this is what I’m getting

or

That’s a different issue related to the Visualizer.

Clone your request, and modify the tests tab to only show the code I provided.

Does that part work? Is it extracting the g_Result field. (Check the console logs).

Get this working first.

If you want to consume this in the visualizer, then this gets much more complicated as you have multiple locations in objects in an array within that field.

My recommendation would be to JSON parse that element, and then overwrite the g_Result value in the response with the parsed value, then consume it in the Visualizer. (If you need access to the elements within the g_Result string).

You haven’t posted all of your code in your tests tab, so I can’t quite see why its producing that error, as line 25 doesn’t seem to relate. (Although line 5 looks like it needs cleaning up and splitting onto two separate lines properly).

It also doesn’t seem to match the table headers in the Visualizer which is just showing Field Data, Record ID, and Mod ID.

What fields do you actually want in the Visualizer?

Whole response from Tests:

const response = pm.response.json();

let g_Result = JSON.parse(response.response.data[0].fieldData.g_Result);

console.log(g_Result);

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>

<table class="tftable" border="1">
    <tr>
        <th>Field</th>
        <th>Value</th>
    </tr>
    
    <tr>
        <td>g_Result</td>
        <td>{{response.data[0].fieldData.g_Result}}</td>
    </tr>
    <tr>
        <td>g_userID</td>
        <td>{{response.data[0].fieldData.g_userID}}</td>
    </tr>
    <tr>
        <td>g_appSessionID</td>
        <td>{{response.data[0].fieldData.g_appSessionID}}</td>
    </tr>
    <tr>
        <td>g_Date_30_days</td>
        <td>{{response.data[0].fieldData.g_Date_30_days}}</td>
    </tr>
    <tr>
        <td>g_Date_Minus_10_Days</td>
        <td>{{response.data[0].fieldData.g_Date_Minus_10_Days}}</td>
    </tr>
    <tr>
        <td>g_visitStatusID</td>
        <td>{{response.data[0].fieldData.g_visitStatusID}}</td>
    </tr>
    <tr>
        <td>g_LocationSettingsID</td>
        <td>{{response.data[0].fieldData.g_LocationSettingsID}}</td>
    </tr>
    <tr>
        <td>g_locationID</td>
        <td>{{response.data[0].fieldData.g_locationID}}</td>
    </tr>
    <tr>
        <td>g_auditorType</td>
        <td>{{response.data[0].fieldData.g_auditorType}}</td>
    </tr>
    <tr>
        <td>g_auditType</td>
        <td>{{response.data[0].fieldData.g_auditType}}</td>
    </tr>
</table>
`;

function constructVisualizerPayload() {
    return { response: pm.response.json() }
}

pm.visualizer.set(template, constructVisualizerPayload());

and error

All I need to have human readable JSON from g_Result, thats it. Thanks for your support.

I don’t think the handlebars syntax works in the same way to reference specific indexes.

The error you see in the visualizer is a handlebars specific error and not a Postman error.

I believe you need to use something like:

<td>{{response data.0. fieldData.g_locationID}}</td>

1 Like

Console show the result correctly, so I will do nothing more with VIsualizer. I accept it how it is in console, is enough for me. Thanks Mike

All of the information you are trying to show is in the fieldData element in the response.

So parse directly to that element and then use that for the Visualizer.

For example.

const response = pm.response.json();

let fieldData = response.response.data[0].fieldData;

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>Field</th>
            <th>Value</th>
        </tr>
        
        <tr>
            <td>g_Result</td>
            <td>{{response.g_Result}}</td>
        </tr>
        <tr>
            <td>g_userID</td>
            <td>{{response.g_userID}}</td>
        </tr>
        <tr>
            <td>g_appSessionID</td>
            <td>{{response.g_appSessionID}}</td>
        </tr>
        <tr>
            <td>g_Date_30_days</td>
            <td>{{response.g_Date_30_days}}</td>
        </tr>
        <tr>
            <td>g_Date_Minus_10_Days</td>
            <td>{{response.g_Date_Minus_10_Days}}</td>
        </tr>
        <tr>
            <td>g_visitStatusID</td>
            <td>{{response.g_visitStatusID}}</td>
        </tr>
        <tr>
            <td>g_LocationSettingsID</td>
            <td>{{response.g_LocationSettingsID}}</td>
        </tr>
        <tr>
            <td>g_locationID</td>
            <td>{{response.g_locationID}}</td>
        </tr>
        <tr>
            <td>g_auditorType</td>
            <td>{{response.g_auditorType}}</td>
        </tr>
        <tr>
            <td>g_auditType</td>
            <td>{{response.g_auditType}}</td>
        </tr>
    </table>

`;

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

You can make the Visualizer template a bit simpler using the ‘@key’ and ‘this’ attributes which are available in Handelbars.JS.

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>Field</th>
            <th>Value</th>
        </tr>
        
         {{#each response}}
            <tr>
                <td>{{@key}}</td>
                <td>{{this}}</td>
            </tr>
        {{/each}}
    </table>

`;

This will show exactly the same result as you had previously.

The g_Result field has a lot of data in it, and I don’t know what elements you want to show in relation to locations.

You can update the original response and change the g_Result field from a string to an object using the following.

const response = pm.response.json().data;

response.response.data[0].fieldData.g_Result = JSON.parse(response.response.data[0].fieldData.g_Result);

let fieldData = response.response.data[0].fieldData;

console.log(fieldData);

This means you could now target those individual elements in the Visualizer if you wanted to, but we would need details on what you want to show.

1 Like