0

I have two web services generating JSON output. Both services are using different technologies but are supposed to generate the exact same output.

I want to check if this output is exactly the same in the browser environment. I would really prefer to simple compare them as string, but the JSON output is not sorted.

I can convert JSON string to objects and then iterate over their keys to check the equivalence but that is basically a n^2 algorithm.

I was wondering if there is any quicker or better way. Something that browser environment already provides.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • 5
    No, the browser does not provide a deep equality check. You can write your own, however: http://stackoverflow.com/q/13142968/139010 – Matt Ball Jun 14 '13 at 00:15
  • It's worth mentioning that that probably won't be an O(n^2) algorithm because key accesses in JavaScript are constant-time. Iterate the keys in object A and make sure each exists (and matches) in object B, then iterate object B checking against object A. No need for a nested loop. – Matt Patenaude Sep 12 '13 at 22:51

1 Answers1

0

At the moment there is no such method provided by the browsers to do a deep comparison. WE have to do it ourselves or using some non native library.