You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow field values to be ignored by specifying a JSON path #15
We often have a requirement where we're not interested in the value of a field, but we do care that the field exists. This might be an autogenerated ID or a timestamp for example.
It would be useful to specify the field(s) as a JSON path expression so that we can be selective in which fields get ignored.
Thinking some more, we probably want to make this feature more flexible than just "ignore a field's value". From a hamcrest point-of-view, it would be good if we could say to JSONassert, "compare these two documents, but if you see a node matching , ignore it". That way, I can make my assertions on particular paths separately. Something like this:
assertThat(myActualDocument, both(
sameJSONAs(myExpectedDocument).ignoringPath("$.library.book.id"))
.and(path("$.library.book").hasField("id"))) // I'm making this syntax up here ;-)
I get it now. I think the concept is useful. My main question now, is what is the proper syntax? Your example almost reminds me of JPA, which is not a bad convention. Still, it is pretty different from the straight-forward "compare X with Y" of JUnit.
One other thing, JSONAssert should already be able to figure out whether a node matches something like "$.library.book.id" without requiring a 3rd-party library. If you want to get more sophisticated with the JSON queries, you might need something else, but integration may prove tricky.
Right, my example was written in a hamcrest style (since I'm writing the hamcrest-json library). I'm not yet sure how to surface this feature in JSONassert itself but you're right, we should probably stick with the existing style. If we /do/ want to integrate a third party JSON path library, we might want to make it an optional dependency. One of the good things about JSONassert is that it needs almost no dependencies!
I need this ignore field functionality, but it seems like it might take a while to agree and finalize this issue, meanwhile i would like to get benefit of using JSONCompareResult,
You're right, this feature isn't done yet but it's good to know I'm not the only one that needs it ;-) What in particular are you looking for from JSONCompareResult?
I was hoping to get list of failure from _fieldFailures and exclude some fields manually but after a bit of investigation currently it only holds the failures on value comparison, all the missing, and unexpected messages are being appended to actual message String so i can not have those fields as a list.
Yes, my idea for JSONCompareResult is that it should provide you with a model of the failures (including missing fields, unexpected fields and mismatched fields). Doing that was higher up my TODO list, so I will try and get round to that one 😃
Constraint gives us theoretically infinite flexibility in terms of what we want to throw into the calculation.
To take it further, we could even consider making JSONCompareMode a child of JSONConstraint to reduce the number of variables. In that case the signature could look like this (I've renamed some classes, but we'd make sure we do this in a way to maintain backwards compatibility with previous releases):
@carterpage At the risk of sounding like a fanboy, I think the API would start to look a lot like Hamcrest's, only Hamcrest is already very composable and has lots of useful features that already exist ;-) (see my earlier example)
@btiernay As a workaround, perhaps you could set the values in the actual and expected documents to a known magic value such as ""?
@hertzsprung I really don't see a difference between (a) ignoring added fields, (b) ignoring array element ordering, and (c) ignoring specific properties. That is to say, if you can make a case for one, you can make a case for all. I could do as you suggest, but then again, I could also do that for (a) and (b).
I could also fork the project and add this feature myself, but I'd much rather contribute to this one as it is already established. Since the need for this feature seems common with many practical applications, why not support it?
The need for (c) comes from the fact that JSON objects do not have an encapsulated equals() method as do classes in languages such as Java. In Java, for instance, one can implement the equals() method to ignore certain fields. In JSON, this must be extrinsic (and thus customizable in assertEquals). This is the motivation here and it seems a reasonable request.
@btiernay It's agreed we need this functionality. We just need to figure out the "right" way to express the functionality. We've been a little slow over the holidays, but we'll be ramping up on features and bugs in the coming weeks. Barring any surprises we'll get this out in a 1.2 in the next month.
I wonder if a more flexible approach might be to allow the developer to specify a wildcard sequence in the character string. The wildcard sequence could be an optional final argument to the assertEquals() method, or a component of the proposed JSONConstraint class.
So let's say you want to check the contents of a response, but you're not interested in the exact value of the (DB-generated) user id:
Potentially less error-prone. You can simply compose your expected document with wildcards, instead of having to compose one or more ignore-paths, and cross check them against the document.
More flexible. Suppose you have a collection of elements, and you want to precise-match a field on some elements of the collection, but wild-card match the same field on other elements of the connection. You can do that with wildcard matching, but not with the ignore-path approach.
I have same requirement and have been googling from long time. i have read the comments but couldnt understand if the issue is fixed, or trying with some solution?
if fixed, can some one please let me know how to ignore fields.
I found my way here looking for partial ignore but after reading @ittiel's solution I implemented my own comparator that looked for specific place holder tokens and did a different type of value comparison.
packagecom.cloudbank.account.endtoend;
importorg.json.JSONException;
importorg.skyscreamer.jsonassert.JSONCompareMode;
importorg.skyscreamer.jsonassert.JSONCompareResult;
importorg.skyscreamer.jsonassert.comparator.DefaultComparator;
importjava.util.UUID;
publicclassDynamicValueTokenComparatorextendsDefaultComparator {
publicDynamicValueTokenComparator(JSONCompareModemode) {
super(mode);
}
@OverridepublicvoidcompareValues(Strings, Objecto, Objecto1, JSONCompareResultjsonCompareResult) throwsJSONException {
// --- ! This could be a stack of things looking at tokens like <UUID> <DATE> etc// A dynamic UUID is expectedif (o.toString().equals("<UUID>")) {
// If yes then the corresponding UUID must be validtry {
UUID.fromString(o1.toString());
} catch (IllegalArgumentExceptione) {
thrownewJSONException("Invalid UUID for field " + s);
}
// Return - don't apply default value checkingreturn;
}
super.compareValues(s, o, o1, jsonCompareResult);
}
}
I recently found this library JsonUnit which has the exact functionality check it out
Reacted by Ittiel, Srikar, Oscar Octavio Bravo Medina, Daniele Orler, Adi Srini, Jonasz, Julian Kolodzei, Matthew Pearson, maximus321, sh-at-cs and 4 more
I use this library and created an extended version for a custom Partial JSON assertion that I needed. I have that jar created for updated library in this location. Feel free to use it and to give me feedback on that.
Reacted by Hielke de Vries, Trần Tiến Đức (Duc Tran), Jakub Kalicki, Till Voß, tomgeraghty3, Saransh, Martin, victokoh, Lukasz Gryzbon, Pavlina Drosos and 3 more
Activity
carterpage commentedon Oct 16, 2012
Any idea what this might look like to a programmer?
hertzsprung commentedon Nov 4, 2012
Thinking some more, we probably want to make this feature more flexible than just "ignore a field's value". From a hamcrest point-of-view, it would be good if we could say to JSONassert, "compare these two documents, but if you see a node matching , ignore it". That way, I can make my assertions on particular paths separately. Something like this:
Or we could maybe have
When I get some time, I'm going to play with https://github.com/jayway/JsonPath and see what might work.
carterpage commentedon Nov 6, 2012
I get it now. I think the concept is useful. My main question now, is what is the proper syntax? Your example almost reminds me of JPA, which is not a bad convention. Still, it is pretty different from the straight-forward "compare X with Y" of JUnit.
One other thing, JSONAssert should already be able to figure out whether a node matches something like "$.library.book.id" without requiring a 3rd-party library. If you want to get more sophisticated with the JSON queries, you might need something else, but integration may prove tricky.
hertzsprung commentedon Nov 6, 2012
Right, my example was written in a hamcrest style (since I'm writing the hamcrest-json library). I'm not yet sure how to surface this feature in JSONassert itself but you're right, we should probably stick with the existing style. If we /do/ want to integrate a third party JSON path library, we might want to make it an optional dependency. One of the good things about JSONassert is that it needs almost no dependencies!
ibrahimover commentedon Nov 29, 2012
Hi,
I need this ignore field functionality, but it seems like it might take a while to agree and finalize this issue, meanwhile i would like to get benefit of using JSONCompareResult,
Is there any date set for 1.1.2 release?
hertzsprung commentedon Nov 29, 2012
You're right, this feature isn't done yet but it's good to know I'm not the only one that needs it ;-) What in particular are you looking for from JSONCompareResult?
ibrahimover commentedon Nov 30, 2012
I was hoping to get list of failure from _fieldFailures and exclude some fields manually but after a bit of investigation currently it only holds the failures on value comparison, all the missing, and unexpected messages are being appended to actual message String so i can not have those fields as a list.
hertzsprung commentedon Nov 30, 2012
Yes, my idea for JSONCompareResult is that it should provide you with a model of the failures (including missing fields, unexpected fields and mismatched fields). Doing that was higher up my TODO list, so I will try and get round to that one 😃
carterpage commentedon Dec 5, 2012
How about something like adding this signature to JSONAssert:
It could work something like this:
Constraint gives us theoretically infinite flexibility in terms of what we want to throw into the calculation.
To take it further, we could even consider making JSONCompareMode a child of JSONConstraint to reduce the number of variables. In that case the signature could look like this (I've renamed some classes, but we'd make sure we do this in a way to maintain backwards compatibility with previous releases):
And it could work like this in a simple case:
Or:
I imagine the ability to chain constraints like this to make them easier to write:
btiernay commentedon Jan 13, 2013
I too need this feature for the same reason (generated id fields). Any progress on this feature?
hertzsprung commentedon Jan 13, 2013
@carterpage At the risk of sounding like a fanboy, I think the API would start to look a lot like Hamcrest's, only Hamcrest is already very composable and has lots of useful features that already exist ;-) (see my earlier example)
@btiernay As a workaround, perhaps you could set the values in the actual and expected documents to a known magic value such as ""?
btiernay commentedon Jan 13, 2013
@hertzsprung I really don't see a difference between (a) ignoring added fields, (b) ignoring array element ordering, and (c) ignoring specific properties. That is to say, if you can make a case for one, you can make a case for all. I could do as you suggest, but then again, I could also do that for (a) and (b).
I could also fork the project and add this feature myself, but I'd much rather contribute to this one as it is already established. Since the need for this feature seems common with many practical applications, why not support it?
The need for (c) comes from the fact that JSON objects do not have an encapsulated equals() method as do classes in languages such as Java. In Java, for instance, one can implement the equals() method to ignore certain fields. In JSON, this must be extrinsic (and thus customizable in assertEquals). This is the motivation here and it seems a reasonable request.
I should also add, FEST assertions FTW :).
carterpage commentedon Jan 27, 2013
@btiernay It's agreed we need this functionality. We just need to figure out the "right" way to express the functionality. We've been a little slow over the holidays, but we'll be ramping up on features and bugs in the coming weeks. Barring any surprises we'll get this out in a 1.2 in the next month.
btiernay commentedon Jan 28, 2013
@carterpage Sorry, I guess I misread this thread :) Looking forward to it!
cepage commentedon Jan 30, 2013
I wonder if a more flexible approach might be to allow the developer to specify a wildcard sequence in the character string. The wildcard sequence could be an optional final argument to the assertEquals() method, or a component of the proposed JSONConstraint class.
So let's say you want to check the contents of a response, but you're not interested in the exact value of the (DB-generated) user id:
The advantages of this approach are:
41 remaining items
Naveen1836 commentedon Oct 20, 2016
Hi,
Apologies for asking same question again.
I have same requirement and have been googling from long time. i have read the comments but couldnt understand if the issue is fixed, or trying with some solution?
if fixed, can some one please let me know how to ignore fields.
praveen133t commentedon Oct 20, 2016
@Naveen1836 : Pls see my reply (two posts above yours). I was able to implement something that way
Naveen1836 commentedon Oct 20, 2016
@praveen133t:
Can you send me the code, i mean class which you are extending that code to naveenavula1989@gmail.com
carterpage commentedon Oct 30, 2016
Anyone want to submit a PR for partial ignore? I can add to 1.4.1.
Implements partial ignore skyscreamer#15
bkdonline commentedon Jul 18, 2017
Partial compare is a must have feature in the world of REST APIs. If we can merge this in time for 1.5.1, it would be very useful. @carterpage
benwaine commentedon Nov 17, 2017
I found my way here looking for partial ignore but after reading @ittiel's solution I implemented my own comparator that looked for specific place holder tokens and did a different type of value comparison.
Trying validate this:
In the test:
JavaOPs commentedon Dec 19, 2017
Maybe it will be helpful: in my Spring controller tests I've made workaround for this problem: https://stackoverflow.com/a/47896806/548473
SrikarNanduri commentedon Jan 22, 2018
+1 for this feature - I have a requirement for this feature
SrikarNanduri commentedon Jan 23, 2018
I recently found this library JsonUnit which has the exact functionality check it out
arundharmar commentedon Feb 3, 2018
I use this library and created an extended version for a custom Partial JSON assertion that I needed. I have that jar created for updated library in this location. Feel free to use it and to give me feedback on that.
VegetaPn commentedon May 22, 2019
+1 for this feature. Any progress on this feature ?
Ripul89 commentedon Jul 3, 2019
Hi All, I am not getting dependency for JAConstraint
dilipsonu commentedon Feb 5, 2020
+1 for this feature
parawanderer commentedon Mar 23, 2025
Hi, I have implemented support for this in this MR: #201
Curious to see if anybody had any feedback or suggestions on it?