How to Compare Two Json Have the Same Properties Without Order

I want to compare two json objects excluding one of the keys from it

Thanks everyone for quick responses on my question but below was an easy method from which I could implement the above logic

import omit from "lodash/omit";
import isEqual from "lodash/isEqual";

let x = {a: 5, b: 6, c: "string"},
y = {a: 5, b: 8, c: "string"}

result = isEqual(omit(x, ['c']), omit(y, ['c']))

Comparing two complex JSON arrays

With help of every() and some(), you can achieve your task.

let JSON3 = { products:[{"productname":"product1","productversion":"1.0","features":[{"featurename":"feature1","featureversion":"1.0"}] }, {"productname":"product2","productversion":"2.0","features":[{"featurename":"feature2","featureversion":"2.0"}] },{"productname":"product3","productversion":"3.0","features":[{"featurename":"feature4","featureversion":"4.0"}] }] };
let JSON4 = { products: [{"productname":"product2","productversion":"2.0","features":[{"featurename":"feature5","featureversion":"5.0"}] },{"productname":"product1","productversion":"1.0","features":[{"featurename":"feature1","featureversion":"1.0"}] },{"productname":"product3","productversion":"3.0","features":[{"featurename":"feature3","featureversion":"3.0"}] }] };
var result = JSON3.products.every(k=>JSON4.products.some(d=>d.productname ==k.productname && d.features.every(s=>k.features.some(l=>l.featurename==s.featurename))));
console.log(result);

Testing two JSON objects for equality ignoring child order in Java

As a general architectural point, I usually advise against letting dependencies on a particular serialization format bleed out beyond your storage/networking layer; thus, I'd first recommend that you consider testing equality between your own application objects rather than their JSON manifestations.

Having said that, I'm currently a big fan of Jackson which my quick read of their ObjectNode.equals() implementation suggests does the set membership comparison that you want:

public boolean equals(Object o)
{
if (o == this) return true;
if (o == null) return false;
if (o.getClass() != getClass()) {
return false;
}
ObjectNode other = (ObjectNode) o;
if (other.size() != size()) {
return false;
}
if (_children != null) {
for (Map.Entry<String, JsonNode> en : _children.entrySet()) {
String key = en.getKey();
JsonNode value = en.getValue();

JsonNode otherValue = other.get(key);

if (otherValue == null || !otherValue.equals(value)) {
return false;
}
}
}
return true;
}

How to compare two JSON objects with the same elements in a different order equal?

If you want two objects with the same elements but in a different order to compare equal, then the obvious thing to do is compare sorted copies of them - for instance, for the dictionaries represented by your JSON strings a and b:

import json

a = json.loads("""
{
"errors": [
{"error": "invalid", "field": "email"},
{"error": "required", "field": "name"}
],
"success": false
}
""")

b = json.loads("""
{
"success": false,
"errors": [
{"error": "required", "field": "name"},
{"error": "invalid", "field": "email"}
]
}
""")

>>> sorted(a.items()) == sorted(b.items())
False

... but that doesn't work, because in each case, the "errors" item of the top-level dict is a list with the same elements in a different order, and sorted() doesn't try to sort anything except the "top" level of an iterable.

To fix that, we can define an ordered function which will recursively sort any lists it finds (and convert dictionaries to lists of (key, value) pairs so that they're orderable):

def ordered(obj):
if isinstance(obj, dict):
return sorted((k, ordered(v)) for k, v in obj.items())
if isinstance(obj, list):
return sorted(ordered(x) for x in obj)
else:
return obj

If we apply this function to a and b, the results compare equal:

>>> ordered(a) == ordered(b)
True


Related Topics



Leave a reply



Submit