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
Testing two JSON objects for equality ignoring child order in Java [closed]
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 strings when the order of entries keep changing
Jackson Json parser has a nice feature that it can parse a Json String into a Map. You can then query the entries or simply ask on equality:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
public class Test
{
public static void main(String... args)
{
String input1 = "{\"state\":1,\"cmd\":1}";
String input2 = "{\"cmd\":1,\"state\":1}";
ObjectMapper om = new ObjectMapper();
try {
Map<String, Object> m1 = (Map<String, Object>)(om.readValue(input1, Map.class));
Map<String, Object> m2 = (Map<String, Object>)(om.readValue(input2, Map.class));
System.out.println(m1);
System.out.println(m2);
System.out.println(m1.equals(m2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output is
{state=1, cmd=1}
{cmd=1, state=1}
true
Groovy compare two json with unknown nodes names and values
Just compare the slurped maps:
def map1 = new JsonSlurper().parseText(document1)
def map2 = new JsonSlurper().parseText(document2)
assert map1 == map2
Related Topics
Save Variables in Every Iteration of for Loop and Load Them Later
Pyspark Regexp_Replace With List Elements Are Not Replacing the String
Python Convert Comma Separated List to Pandas Dataframe
Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
Using a Pandas Dataframe as a Lookup Table
Python: Draw Line Between Two Coordinates in a Matrix
Using Selenium in Python to Save a Webpage on Firefox
How to Sort a Single String Output in Ascii Descending Order Through a Function
Python - Split Array into Multiple Arrays
Python Creating Dictionary from Excel Data
Invalidargumenterror: Logits and Labels Must Have the Same First Dimension Seq2Seq Tensorflow
Drop Rows With All Zeros in Pandas Data Frame
How to Save All the Variables in the Current Python Session
How to Select All Elements Greater Than a Given Values in a Dataframe
Delete Rows Containing Numeric Values in Strings from Pandas Dataframe
How to Constantly Run Python Script in the Background on Windows
How to Plot Date and Time in X Axis Against Y Value (Python)