If (everyDesiredObjectPropertyMustExistInCurrentObject) Var msg = "Desired property '" + property + "' does not exist in current JSON object" System.error("Desired value does not match with current value") bug("Desired value matches with current value") If (desiredJsonSubObject = currentJsonObject) If (!isJsonEqual(JSON.stringify(desiredJsonSubObject), JSON.stringify(currentJsonObject), recursionCount)) return false If (typeof desiredJsonSubObject = 'object' & desiredJsonSubObject != null) Desired value '"+desiredJsonSubObject+"' Current value '"+currentJsonObject+"'") If (currentJsonObject.hasOwnProperty(property))
If (desiredJsonSubObject.hasOwnProperty(property)) Var currentJsonObject = JSON.parse(currentJsonString) įor (var property in desiredJsonSubObject) Var desiredJsonSubObject = JSON.parse(desiredJsonString) bug("isJsonEqual: " + isJsonEqual) įunction isJsonEqual(desiredJsonString, currentJsonString, recursionCount) Var isJsonEqual = isJsonEqual(desiredJsonObjectAsString, currentJsonObjectAsString, 0) Var everyDesiredObjectPropertyMustExistInCurrentObject = true Var desiredJsonObjectAsString = "" // any complex json object This function returns true or false, but I am sure you can adapt the function in that way so it fits your needs.Īlso this is not a high sophisticated implementation, so it might not work in every use case. I am not sure if this helps but I've written also an action which compares two complex JSON objects. It also contains the provision to load JSON data via URL. It is quite a useful tool to clean up your JSON, remove whitespace and format JSON etc.
Javascript json compare code#
Your XML suggestion might work too but you'd be also writing code for that It is a robust JSON viewer that allows you to paste JSON in the text tab and then look at the tree structure in the viewer tab.
Javascript json compare full#
If you want a full diff and report then you would have to code a specific parser / inspector function I'd imagine. Or, copy out the relevant bits to a script/action in vRO and get your comparison done
Javascript json compare free#
If you want to be able to compare objects regardless of internal field ordering then you might look at Lodash - it's a free JS module built for Websites and Node.js projects but you *should* be able to load it into a vRO context and use it's functionality. Converting objects to string like this can be expensive if the objects are large The simple comparison here will only return true if all the internal fields are in the same order (it's comparing text after all)ģ. That is why 'Field E' and its value is not returned. Purists might argue that the test there should be "="Ģ. Note that it is using jsonstringA as the base of the comparison, so the function returns only the fields missing and values of jsonStringB. If(JSON.stringify(obj1) = JSON.stringify(obj2))ġ. Or are you simply interested in checking if the two objects are equal? If the latter then this is will give a simple true/false When you say compare do you really mean diff as you might in Unix?