How can I compare if two json structures are the same in scala?
For example, if I have:
{
resultCount: 1,
results: [
{
artistId: 331764459,
collectionId: 780609005
}
]
}
and
{
results: [
{
collectionId: 780609005,
artistId: 331764459
}
],
resultCount: 1
}
They should be considered equal
You should be able to simply do
json1 == json2
, if the json libraries are written correctly. Is that not working for you?This is with spray-json, although I would expect the same from every json library:
Spray-json uses an immutable scala
Map
to represent a JSON object in the abstract syntax tree resulting from a parse, so it is justMap
's equality semantics that make this work.Can confirm that it also works just fine with the Jackson library using
==
operator:spray-json
is definitely great, but I useGson
since I already had dependency onGson
library on my project. I am using these in my unit tests, works well for simple json.Calling the method
compare_2Json(str1,str2)
will return a boolean value. Please make sure that the two string parameters are json. Welcome to use and test.