As mentioned in my previous post on Ajax I came upon JSON quite by accident and was actually using it for some time without having any idea.
So it was wonderful to discover that instead of the needlessly verbose:
I could simply use the much more concise object literal syntax to declare an object instead:
Lovely. Henceforth I adopted this approach in my code as I'm generally a believer that brevity is best.
However, I was reading the the fantastic Dave Ward's Encosia blog which on this occasion was talking about the troubles of UpdatePanels (a subject close to my heart by the way) and more interestingly the use of PageMethods in ASP.NET. This is what he said that made me prick up my ears:
JSON is a lightweight SOAP eh? I've used SOAP. I wonder if I could use this....
- JSON.stringify(myObject) - take an object and make me a JSON string. (and by the way isn't "stringify" just the loveliest method name ever?)
- JSON.parse(myJSONString) - take a JSON string and make me an object
I've also demonstrated this using the Chrome Console:
Crockford initially invented/discovered JSON himself and wrote a little helper library which provided a JSON object to be used by all and sundry. This can be found here: JSON on GitHub
Because JSON was so clearly wonderful, glorious and useful it ended up becoming a part of the EcmaScript 5 spec (in fact it's worth reading the brilliant John Resig's blog post on this). This has lead to JSON being offered natively in browsers for quite some time. However, for those of us (and I am one alas) still supporting IE 6 and the like we still have Crockfords JSON2.js to fall back on.