zgrossbart / jdd

A semantic JSON compare tool
http://www.jsondiff.com
Apache License 2.0
1.05k stars 183 forks source link

Enhancement: Add option to ignore array order #3

Open vbro opened 7 years ago

vbro commented 7 years ago

This is for situations where the order of elements in an array has no semantic meaning. Example 1. { "image1": { "filesize":123456, "filename":"image1.jpg", "tags":["tech", "iphone"] }, "image2": { "filesize":987654, "filename":"image2.jpg", "tags":["nature", "california", "yosemite"] } } AND { "image1": { "filesize":123456, "filename":"image1.jpg", "tags":["iphone", "tech"] }, "image2": { "filesize":987654, "filename":"image2.jpg", "tags":["yosemite", "nature", "california"] } }

Example 2. (more complex version of example above) { "image1": { "filesize":123456, "filename":"image1.jpg", "tags":[{"tech":[1, 2]}, {"iphone":[3, 4, 5]}] }, "image2": { "filesize":987654, "filename":"image2.jpg", "tags":["nature", "california", "yosemite"] } }

{ "image1": { "filesize":123456, "filename":"image1.jpg", "tags":[{"iphone":[5, 3, 4]}, {"tech":[2, 1]}] }, "image2": { "filesize":987654, "filename":"image2.jpg", "tags":["yosemite", "nature", "california"] } }

raqbit commented 5 years ago

I'm currently working on parsing an API and json-stringifying it again to check if I parse everything correctly.

This API returns several arrays with non-semantic ordering which changes every request.

Adding the ability to ignore array ordering would make it a lot easier to debug :+1:

zgrossbart commented 5 years ago

Thank you @Raqbit. I've been going back and forth about this. The problem is that arrays in JSON have a semantically significant ordering so they are different. I'm still working out the best way to handle this.

LucasAstol commented 4 years ago

Hi @zgrossbart

I really appreciate your tool, it helped me test large JSONs and also code a test for JSON comparisson in Postman. However I seem to have this same issue with your tool and with JS libraries, specially lodash when using _.isEqual method.

By your code I see you have written the functions yourself, so I hope you can solve this issue since it would be really helpful. I wouldn't be abe to copy your code due to my knowledge limitations but would be more than glad to use your tool for testing.

Another really cool thing would be to have an API that when sending two JSON objects one can obtain the comparisson results in a response :D

Keep up the good work

Best.

zgrossbart commented 4 years ago

@LucasAstol, can you please give me some more information. I'm not sure what you're asking.

LucasAstol commented 4 years ago

Sorry it was a bit messy.

Mainly I'd like to know if there's a chance that you have solved this particular issue on the array order as @vbro mentions. I happen to have really large JSONs with arrays of objects that contain other arrays, and so on... Thus the json-diff fails event hough the JSON objects are semantically equal.

Moreover I'm proposing that it would be nice to have an API so we can directly send JSONs and get the validation results.

Best.

zgrossbart commented 4 years ago

That makes sense. Thank you @LucasAstol.

The problem here is that those JSON objects aren't semantically equal.

{
    "my array": [
        "one",
        "two"
    ]
}

isn't the same as

{
    "my array": [
        "two",
        "one"
    ]
}

You could sort the arrays and then do the compare, but that's not an isEqual function, that's specialized business logic.

A few people have taken this project and built NodeJS utilities out of it. You might want to check those out if you're looking for an API.

pochopsp commented 1 year ago

@zgrossbart Sorry to bring this up again after all this time, but I'd like to ask if something like that would actually be possible. I am working on an access protocol based on OAuth2 which uses very complex json objects NOT considering order inside array, so for it ["a", "b"] is the same as ["b", "a"].

I love your tool and I use it a lot, but I would find it even more useful if it had an option to choose wether it should consider order in arrays.

As regards the web interface, it could just be a flag saying "Compare arrays ignoring order" which would be always disabled unless user wanted to use it.

zgrossbart commented 1 year ago

@pochopsp, thanks for reaching out about this. If your JSON data has a set of values where the order doesn't matter then why are you using an array? The easiest way to implement this feature would be to sort the arrays before comparing them. The problem is that brings up a lot of issues.

For example: does

{
    "my array": [
        "one",
        "two"
    ]
}

equal

{
    "my array": [
        "one",
        "",
        "two"
    ]
}

What about

{
    "my array": [
        "one",
        null,
        "two"
    ]
}

A lot of these questions become unanswerable since you need to make specific decisions that aren't covered by the JSON specification.

If you have enough of a specific need for this then you could change the code locally to make this happen. I'd be happy to point you in the right direction.

pochopsp commented 1 year ago

Hi @zgrossbart . Thanks for your kind reply. I'm using an array because the protocol specify it. We only need a list of strings, no matter their order.

These are the specs: https://openid.net/specs/openid-connect-federation-1_0.html See section 5.1.2 (metadata policy), there are a lot of examples of arrays in which order doesn't matter.

Like this one: image That list only states which algorithms are supported, their order doesn't matter.

As regards your example, in my opinion the three versions of "my array" are all different between each other. As far as I'm concerned, I'd consider "" to be different than null, and both certainly have a different meaning than a missing value.

Still, you're right, the official JSON specifications don't cover these cases, so it's legit to think that it would be too messy/ambiguous to implement this feature.

zgrossbart commented 1 year ago

Thank you for understanding @pochopsp. If you have a one-time need I'm happy to point you to how to change the code to do that.