I needed to compare two JSONs to check the equality of production JSON and JSON in development system. So here is one function to do so.
I assume one JSON is in correct format and check if the other is in correct format. Also, the function uses lodash, a JavaScript utility library delivering consistency, modularity, performance, & extras.
The function does:
Print out missing property.
Print out if the data type is different.
Prunt out if the value is different.
Print out if an object in array is missing
Here is the code on Github gist. I also put code in this blog just in case if you want to take a look.
objectKeys is used to detect an object in array of objects. Because JSON is likely to have random order of objects, the function needs a property to find out the same object in array. It takes property containing array in key and property name to compare objects in value.
excludedKeys, any property name in this array does not check the equality of the values. It does check missing property and different data type though.
On a side note, there are nice npm package and online checker. Try them first. If they do not work as expected, it is the time to start writing your own function. I don’t think my function answers all of what you want to do. But it should give you some hook to start with. If you find any bugs or unexpected behaviors, just let me know via email: koheiarai.1[at]]gmail.com