Comparing responses

Hi everyone,
I’m a newbie that is slowly finding my feet with postman, I’m not good with code either (which sucks but something for me to work on in time) - hoping to do some of the postman courses mentioned above (when time permits). Please forgive me if I use the wrong terminology here.

In the meantime I am wondering if anybody could explain to me in very simple terms where I am going wrong, I have tried some of the varying examples in this thread, but can’t seem to get the key I want to write out to a variable.

I am trying to access the data that is returned in the response body from a basic get request.

Response

In the Test section of GET Request, I am wanting to validate the ‘mPP’ is what I expect it to be, to I can ensure that this value is what I need it to be with a post request prior to running the GET.

I added some snippets where I can search for the value using the below, but it feels clunky.
pm.test(“Check Value is correct 4.88”, function () {
** pm.expect(pm.response.text()).to.include(“4.88”);**
});

I am now trying to do this using the below, but I get an error.
let responseData = pm.response.json();
console.log(responseData);
pm.environment.set(“mPP”, responseData.hotels[1].mPP[0]);

I get this response data, and I am wanting to access the mPP value in yellow, but trying the above and variations, no what I do, I can seem to get it set grab the value and set the env variable and get varying errors depending on what I try e.g.,
TypeError: Cannot read properties of undefined (reading ‘mPP’)

Hope I have explained this well enough.
As I say I will look at the postman learning stuff when time permits for me to go through it all.

Thank you in advance for any help that may be offered to me.

JSON is key/value pairs but the values can be objects and arrays.

You haven’t fully posted your response. Please don’t use screenshots. Cut and paste the response and use the preformatted text option in the forum editor.

It appears that your response has a top level object called hotels, which is an array, and then within each hotel, you have another array called accom which only has one object in it, and within that object you have a pricem array which has two objects in it.

First step is to parse the response.

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

You then need to target the Hotels array.

console.log(response.hotels);

To target objects within the array, you need to use the array index. Array indexes in JavaScript start at 0.

console.log(response.hotels[0]);

You then target the accom array.

console.log(response.hotels[0].accom[0]);

Then targert the pricem array and the value you want is in the first object.

console.log(response.hotels[0].accom[0].pricem[0]);

Finally, you want the value for the mPP key.

console.log(response.hotels[0].accom[0].pricem[0].mPP);

You can define this as a variable to use in your assertion.

const response = pm.response.json();

let pricem = response.hotels[0].accom[0].pricem[0].mPP;

pm.test("Check Value is correct 4.88", function () {
   pm.expect(pricem).to.eql(4.88);
});

I would highly recommend going throught the Postman Learning Centre materials, and also looking up JSON stuctures on the web.

This should be your starting point before posting queries on forums.

1 Like

Thank you for you in-depth and helpful response - I found it pretty straight forward to understand once I converted everything I had anonymised.

I managed to get this working and put the value in a variable so it can update each time instead of hardcoding what I am expecting so each get request has the correct value.

I take on board what you have said, and will look at things in more depth when time permits.

I also came across a useful tool pointed out by somebody else on a different thread to find the correct JSON Path.

Kind Regards.

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