-
Notifications
You must be signed in to change notification settings - Fork 187
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
Comments
Adds an option to ignore the order of elements in arrays by recursively sorting arrays in the JSON object
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 👍 |
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. |
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. |
@LucasAstol, can you please give me some more information. I'm not sure what you're asking. |
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. 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. |
That makes sense. Thank you @LucasAstol. The problem here is that those JSON objects aren't semantically equal.
isn't the same as
You could sort the arrays and then do the compare, but that's not an 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. |
@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 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. |
@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. |
Hi @zgrossbart . Thanks for your kind reply. These are the specs: https://openid.net/specs/openid-connect-federation-1_0.html Like this one: 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. |
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. |
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"]
}
}
The text was updated successfully, but these errors were encountered: