VMware Cloud Community
anba89
Enthusiast
Enthusiast

vRO - compare two complex JSON

I need to compare two complex JSON Body with vRO.

Does anyone have any idea how I can do this?

It's about comparing two NSX DFW configurations. So XML would work, too, if it's easier to compare.

Tags (1)
0 Kudos
5 Replies
daphnissov
Immortal
Immortal

Probably best asked in the vRO sub-forum.

0 Kudos
anba89
Enthusiast
Enthusiast

Thanks. I moved it to the vRO sub-forum.

0 Kudos
eoinbyrne
Expert
Expert

When you say compare do you really mean diff as you might in Unix?

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

if(JSON.stringify(obj1) == JSON.stringify(obj2))

There are a few notes on this too

1.  Purists might argue that the test there should be "==="

2.  The simple comparison here will only return true if all the internal fields are in the same order (it's comparing text after all)

3.  Converting objects to string like this can be expensive if the objects are large

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. Or, copy out the relevant bits to a script/action in vRO and get your comparison done

If you want a full diff and report then you would have to code a specific parser / inspector function I'd imagine. Your XML suggestion might work too but you'd be also writing code for that

0 Kudos
anba89
Enthusiast
Enthusiast

Thank you very much for your feedback.

With compare I mean a diff to get all the differences and not only a true or false.

I will check Lodash and I'll get back to you.

0 Kudos
LukasWe
Enthusiast
Enthusiast

Hi,

I am not sure if this helps but I've written also an action which compares two complex JSON objects.

This function returns true or false, but I am sure you can adapt the function in that way so it fits your needs.

Also this is not a high sophisticated implementation, so it might not work in every use case. But it should be a good starting point Smiley Happy

var desiredJsonObjectAsString = "{ \"hello\": \"world\", \"vro\": { \"version\": \"7.6\" } }"; // any complex json object
var currentJsonObjectAsString = "{ \"hello\": \"world\", \"vro\": { \"version\": \"7.5\" } }"; // any complex json object
var everyDesiredObjectPropertyMustExistInCurrentObject = true;

var isJsonEqual = isJsonEqual(desiredJsonObjectAsString, currentJsonObjectAsString, 0);
System.debug("isJsonEqual: " + isJsonEqual);

function isJsonEqual(desiredJsonString, currentJsonString, recursionCount)
{
var desiredJsonSubObject = JSON.parse(desiredJsonString);
var currentJsonObject = JSON.parse(currentJsonString);

for (var property in desiredJsonSubObject)
{
  if (desiredJsonSubObject.hasOwnProperty(property))
  {
   if (currentJsonObject.hasOwnProperty(property))
   {
    System.debug(property + " exists in both objects. Desired value '"+desiredJsonSubObject[property]+"'; Current value '"+currentJsonObject[property]+"'");
   
    if (typeof desiredJsonSubObject[property] === 'object' && desiredJsonSubObject[property] != null)
    {
     recursionCount = recursionCount + 1;
    
     if (!isJsonEqual(JSON.stringify(desiredJsonSubObject[property]), JSON.stringify(currentJsonObject[property]), recursionCount)) return false;
    
     recursionCount = recursionCount - 1;
    }
    else
    {
     if (desiredJsonSubObject[property] == currentJsonObject[property])
     {
      System.debug("Desired value matches with current value");
     }
     else
     {
      System.error("Desired value does not match with current value");
      return false;
     }
    }
   }
   else
   {
    var msg = "Desired property '" + property + "' does not exist in current JSON object";
   
    if (everyDesiredObjectPropertyMustExistInCurrentObject)
    {
     System.error(msg);
     return false;
    }
    else
    {
     System.warn(msg);
    }
   }
    }
}

System.log("Desired properties are equal with current properties");

return true;
}

The output of the code above is this

[2019-06-26 14:57:15.718] [D] hello exists in both objects. Desired value 'world'; Current value 'world'

[2019-06-26 14:57:15.721] [D] Desired value matches with current value

[2019-06-26 14:57:15.723] [D] vro exists in both objects. Desired value '[object Object]'; Current value '[object Object]'

[2019-06-26 14:57:15.726] [D] version exists in both objects. Desired value '7.6'; Current value '7.5'

[2019-06-26 14:57:15.729] [E] Desired value does not match with current value

[2019-06-26 14:57:15.731] [D] isJsonEqual: false

Regards,

Lukas

0 Kudos