I am trying to compare two JSON objects. When a 'key:value' pair order changes, I can use JSON.parse
, and during comparison, the test passes like this:
doc1 = '{
"KayA": "Value_A",
"KeyB": "value_B"
}'
doc2 = '{
"KeyB": "value_B",
"KayA": "Value_A"
}'
doc1 = JSON.parse(doc1)
doc2 = JSON.parse(doc2)
expect(doc1).to eq(doc2) # true
But when the order of a section, an array, or a block of content changes, my assertion fails if I do the same comparison logic like below:
doc1 = '{
"keys": [
{
"KayA": "Value_A",
"KeyB": "value_B"
},
{
"KayC": "Value_C",
"KeyD": "value_D"
}
]
}'
doc2 = '{
"keys": [
{
"KayC": "Value_C",
"KeyD": "value_D"
},
{
"KayA": "Value_A",
"KeyB": "value_B"
}
]
}'
doc1 = JSON.parse(doc1)
doc2 = JSON.parse(doc2)
expect(doc1).to eq(doc2) # false
Is there anyway I can compare even if a block changes?