Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare complete JSON objects

Is there any way to compare 2 json objects using ChaiJS? I want to compare them deeply till the leaf nodes. However, it is better if the code ignore the order of the siblings within the JSON Object - would only validate structure and value and datatype of values. Any help is appreciated.

I just saw the following code, however not sure what is equalsRec

expect({ a: 3, b: {c: '2'} }).to.equalRec({ a: 3, b: {c: 2} }) //expecting false
expect({ a: 3, b: {c: '2'} }).to.equalRec({ b: {c: '2'}, a: 3 }) //expecting true
like image 396
sribasu Avatar asked Jul 14 '26 10:07

sribasu


1 Answers

First of all there is no such thing as "JSON object". You have object literals. Then "ignore the order of the siblings" object keys have no order.

To compare two objects you could use deep flag

expect(obj1).to.be.deep.equal(obj2)
like image 50
Yury Tarabanko Avatar answered Jul 15 '26 23:07

Yury Tarabanko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!