Extract JSON string from response body into Test Results

Hi All,

New to Postman but have an urgent requirement to extract some data for several hundred devices. I am looking to use the runner to extract a version for multiple devices. From what I can see the easiest way would be to get these values displayed into test results so I can download them. I have created the below post test script but the software version is not displayed. I have also tried to use a similar script with const but still fails.

Post test script;
let ips = pm.response.json().data

pm.test(‘IPS Version ${ips}’, function () {
pm.expect(29181.280.s).to.eql(29181.280.s);
});

Response Body;
{
“data”: [
{
“last-updated”: “Mon Jul 22 17:40:24 BST 2024”,
“ips-version”: “29181.280.s”
}
]
}

Any Help would be appreciated.

thanks

1 Like

The test results tab is for tests.

You should use the console logs to extract specific data, or Postman has a handy visualizer tool that allows you to format responses.

If you are new to Postman, you really ought to go to the Learning Centre and learn the basic features of Postman first so you know what the art of the possible is.

When you say, several hundred devices. Do you all mean of the devices in a single request, or do you have to loop through all of the devices because the request only returns a single device.

That test doesn’t make sense by the way. You are just testing the same reference. This should error as it appears this should be a string.

1 Like

Hi Mike,

Thanks for taking the time to reply. I know the test are used for test success etc but for ease it was better to print the outout required into the test so I could get our OOH staff to manually check the results. This is only a shot term fix until the manufactuere fix’s a bug for IPS updates.

I figurered out the code required. Below is the post test script incase anyone else needs the same;

ips = pm.response.json();
var version = ips.data[0][“last-updated”]

pm.test("last updated " +version, function () {

});

So that is just a test with no assertions.

You could at least have an assertion checking that the “last-updated” field exists.

I’m still a bit confused, as that appears to only show one entry.

You mentioned several hundred devices in your initial post.

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