wI2L/jsondiff

[Question] How to compare two JSONs with some fields ignored

polichan opened this issue · 3 comments

Currently, I would like to compare two JSONs with some fields ignored

There is an example to demonstrate my thoughts.

Input:

{
    "code": 0,
    "msg": "OK",
    "data": {
        "uuid": "123",
        "name": "foo",
 	"age": 10
    }
}

some JSON to compare:

{
    "code": 0,
    "msg": "OK",
    "data": {
        "uuid": "456",
        "name": "foo",
 	"age": 10
    }
}

The only difference between the two JSONs is the field uuid.
Therefore, is there a way that jsondiff can ignore the uuid field ? I mean the Patch that returned from jsondiff.Compare() should be nill because the field uuid is ignored.

I'm look forward your response, thanks.

wI2L commented

Hello. The package doesn't know how to exclude a field, it compare the source and dest completely.
I recommend that you edit the json documents before doing the diff by removing the uid field.

Hello. The package doesn't know how to exclude a field, it compare the source and dest completely. I recommend that you edit the json documents before doing the diff by removing the uid field.

Thanks for your reply. Removing the uuid field before doing the diff is not what I want to do.
For those who want to do the same thing just like me, BTW for my example is to do Integration Testing for my own WEB API project, I highly recommend another package called google/go-cmp. It can easily deal with this problem.
Still thanks for your package, it's really great.

wI2L commented

I get it, but not all specific cases can and should be supported directly in the package. It adds in complexity and performance cost.

The scope of go-cmp and jsondiff are not quite the same, since the former is focused on equality comparison of go values, while the latter generates a JSON Patch output. Given the use case that you mentioned, I think that this package was not the best fit for you anyway.