Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add option to ignore array order #3

Open
vbro opened this issue May 1, 2017 · 10 comments
Open

Enhancement: Add option to ignore array order #3

vbro opened this issue May 1, 2017 · 10 comments

Comments

@vbro
Copy link

vbro commented May 1, 2017

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"]
}
}

vbro added a commit to vbro/jdd that referenced this issue May 1, 2017
Adds an option to ignore the order of elements in arrays by recursively sorting arrays in the JSON object
@raqbit
Copy link

raqbit commented Aug 19, 2019

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 👍

@zgrossbart
Copy link
Owner

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
Copy link

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
Copy link
Owner

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

@LucasAstol
Copy link

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
Copy link
Owner

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
Copy link

@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
Copy link
Owner

zgrossbart commented Jun 16, 2023

@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
Copy link

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants