I am using Karate framework and trying to match two feature file json responses are similar, where the date tag has different values so I want to ignore the values but make sure the fields are present.. please find the example below
test1.feature
an example : {"active":true, "createdBy":"2025-03-27T12:35:32.000Z", "version": 2}
test2.feature
an example : {"active":true, "createdBy":"2025-03-25T02:35:32.000Z", "version": 2}
now we do the check using
the above match always fails, not sure how to validate the payloads and ignore the tag values required but same time ensuring the structure is correct.
I know there are other approach where we can go through each tag and validate but consider the payload is huge and complex.
To compare two JSON responses in Karate while ignoring certain field values (like createdBy
), you can remove or ignore those fields before performing the comparison. Karate provides a remove()
function, which can be used to remove specific fields from your JSON objects before performing any assertions.
The solution could be:
Feature: Compare two JSON responses ignoring certain fields
Scenario: Ignore the 'createdBy' field when comparing two responses
# Parse the JSON responses
* def test1Response = response1.json
* def test2Response = response2.json
# Remove the 'createdBy' field from both responses (you can add more fields here if needed)
* def test1WithoutCreatedBy = test1Response.remove('createdBy')
* def test2WithoutCreatedBy = test2Response.remove('createdBy')
# Now match the modified JSONs (ignoring 'createdBy' field)
* match test1WithoutCreatedBy == test2WithoutCreatedBy
# Optionally, ensure both responses have the correct structure (e.g., checking field types)
* match test1Response contains { active: '#boolean', version: '#number' }
* match test2Response contains { active: '#boolean', version: '#number' }