Compare two JSON postman responses using comparison tool

Is it possible to automate two Postman responses in JSON format using any comparison tools?
If yes, any suggestions in script.

You just want to compare that both responses have identical properties and property values?

Hi Allen,

Yes, I want to compare if both are having identical properties and values or not

You could use lodash to do this.

In your first request in the test section you can add the following:

const response = pm.response.json();
pm.environment.set('firstResponse', JSON.stringify(response));

Then in the test section of your second request, you could add:

pm.test('Response is idential to other response', function() {
    const firstResponse = JSON.parse(pm.environment.get('firstResponse'));
    const secondResponse = pm.response.json();

    const isEqual = _.isEqual(firstResponse, secondResponse);
    pm.expect(isEqual).to.be.true;
}

That should do what you’re looking for.

2 Likes

Hi Allen,
thanks for the above one and used it in my script as well.

But I have one query:
1st response:

[{
	"Name": "XYZ",
	"Gender": "Male"}
	{"Group": "Mid",
	"Type": "Early"}]

2nd response:

[
{
	"Name": "XYZ",
	"Gender": "Male",
	}
	{
	"Group": "Mid",
	"Type": "Early"}
	]

When i compare the above ones, my test is failing because of braces mismatch.

How to sort this out?

The first response doesn’t even look like valid JSON to me so if that’s being returned, is that a different problem :smiley:

The second one is also missing a , between the 2 objects in the array :thinking:

1 Like

I have corrected the responses now. I made a typo error.

I use a free online tool called DeltaJSON from DeltaXML. It allows compare and merge, by analysing the structure rather than line by line, so is more effective for JSON files where the properties and values move around in the file. It can give a delta file to for the changes.

To automate the process you could write a script to retrieve the JSON files and then post them to the REST service they provide.

For a simple manual comparison you could use JSON Diff.

Hi. I know it’s been a while since you posted this, but I used this almost exactly as written and it worked perfectly.

I was wondering do you have something like this for and XML response?

Thanks

Glad you found it useful @oberryt12 and welcome back to the forum!

For xml my answer will probably sound a little funny :smile:

Convert the xml to json then compare it the same way I mentioned before. The Postman sandbox includes xml2js, which you can use to do the conversion.

const xml = '<value>test</value>';
pm.test('compare xml', () => {
  const parseString = require('xml2js').parseString;
  parseString(xml, (err, result) => {
    pm.expect(err).to.not.exist;
    pm.expect(result).to.have.property('value', 'test');
  });
});

That should do what you need.

1 Like

Hello Allen ,

I have somewhat similar scenario but here i want to compare the responses which is received in array format and each element of array has some parameters which needs to be compared with another API & again that API has response in array format with each element of array has some parameters but names for that parameter is different in both APIs

Consider the example,
GET API Response for first API array element has parameter name AccountNumber: 12345

GET API Response for second API array element has parameter name Number:12345

i have used below method but not able to make it scalable which can read the parameter name and value from each element of array and compare it with another APIs array elements.

Test tab in Fist GET API
var jsonData = pm.response.json();
var Param1 = jsonData.result[0].AccountNumber
pm.globals.set(“Param1”,Param1);

Test tab in Second GET API
var param2 = jsonData.items[0].Number;
pm.globals.set(“param2”, param2);
pm.test(“For this Service Parameters are same”, function () {
pm.expect(pm.globals.get(“Param1”)).to.eql(pm.globals.get(“param2”));
});

Thank you,
Rahul

@Rdevre

Not sure what you are trying to do there.

Your test is just getting the value of two distinct objects within an array.

The only reason it works is because you are getting the direct values, so it doesn’t matter that the keys are actually different (AccountNumber → Number)

Can you please clarify what you mean by scalable?

If you were testing the array including key names for matching purposes, then this would fail.

You can’t just loop through the array, for it to know that AccountNumber and Number should be matched. (Hence the question about what you mean by scalable).

The previous code examples are using a strict comparison using the Lodash library isEqual function. (Which your example would fail).

Doing a DIFF on an array or JSON is complex and requires external libraries to do it justice, which is why I would just recommend using an online tool for now.

When I say external libraries, they nearly all use node, so you can’t just copy and paste a function off the web that you could import with Eval as they have dependencies that Eval can’t handle.

On a side note, why a global variable? Do you really need that variable available to all workspaces and collections? You’ve already retrieved param2 in the first line of code for the second API, you then set it as a global variable, and then retrieve it again in your test. This seems over the top.

Thank you for your answer Allen,

Yes i want to compare the JSON response of 2 APIs but with the specific parameters which are there for each array element and i was trying to loop through each array element and compare the parameters.

but as you mention it is not possible directly in POSTMAN with the existing libraries.

you are correct set global variable for second API is not required. thanks.