zgrossbart / jdd

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

Fix for https://github.com/zgrossbart/jdd/issues/3 #4

Open vbro opened 7 years ago

vbro commented 7 years ago

Adds an option to ignore the order of elements in arrays by recursively sorting arrays in the JSON object

zgrossbart commented 7 years ago

Hello Valery,

Thank you very much for your contribution. I've heard a request like this one before, but I've always had one question about it. Arrays in JSON do have a very specific meaning. If you don't want the array order to matter then why not use an object instead of an array?

To put it another way, why would you want an unordered array in JSON?

vbro commented 7 years ago

Hi Zack, Thanks for the response. Yes, JSON arrays are by specification, ordered. However, there are real world scenarios where the order of elements in an array is irrelevant. To use an object to represent an un-ordered list, you may have to store the data as keys and pair them with null / placeholder values. Not an ideal solution either. This feature is especially useful when debugging, when you may not have control of the data you're dealing with. For me it's more about curiousity; I'm interested in seeing what the data looks like when the arrays are sorted.

zgrossbart commented 7 years ago

Hi Valery,

That makes a lot of sense, but I need to think about it a little bit. I've been trying to keep this tool really really simple and explaining something like sorting arrays gets tricky. I'm going to consider this for a little while.

Thanks, Zack

vbro commented 7 years ago

Sounds good. There is always a fine balance between adding new features and keeping a product simple and easy to use. Regardless, you've done a good job on such a great product.

evenost-helx commented 7 years ago

We could perhaps indicate in the diff output message that the arrays just have a different order but would have been equivalent if they were sorted?

tw258 commented 1 year ago

Thank you @vbro for your pull request (and of course @zgrossbart for this repo at all). I had the same issue where I wanted to ignore the sorting of the arrays. Your code works just fine for me