JS debug: visually compare two objects or JSONs in browser
Today I was trying to find differences between two objects (coming from JSON API) during debugging in Chrome. First, I was printing them separately using console.log() and then copy-pasting into http://www.jsondiff.com/ for visual inspection. When I did it 3 times I decided I need better approach. So I wrote helper static function (ES6 syntax):
How does it work?
It converts JS objects into JSON string
Encode that string so it can be used in URL
Compose URL to jsondiff.com with both serialized objects as parameters
Print this link into console together with message - so you can have some debugging context.
When executed, it prints following into Chrome/Firefox/MaybeOther Developer Tools console:
If you click on that link, it opens new tab with visual comparison:
Please note that jsondiff.com can even handle scattered keys and missing array entries. It is so cool and does not need to install any special plugins. Handy little trick, don’t you think so?