Saturday, March 23, 2013

JSON: The Undisputed King of Web Interchange APIs

by +Dietrich Schmitz

I have been roaming over the InterTubes in search of various technical information as regards this website.

It can at times be like looking for a 'needle in a haystack' when it comes to pinning down a specific method of getting your website to function in a specific way.

Like for instance, it occurred to me that there ought to be a 'standard way' to query (via some application programming interface or API call) a chunk of information concerning activity taken on the website, be it global website metadata, something about a post or collection of posts or about a specific author--stuff like that. (Credit image right: www.json.org)

Well, the bad news is, there really isn't one 'standard' way.  The good news is, today at least, you have more than one way to obtain that information at your disposal.  But that can create confusion.

In fact, if you do the requisite research as I have (in an effort to answer a few needs here on Linux Advocates), you might at first be overwhelmed by the sheer surplus of data, help websites, how-to's, 'know-it-all' posts that confront you, all bombarding your brain on a Google search adventure.

Fortunately, in the case of Blogger, the documentation for their web application programming interface is right at your fingertips here.

The methods you ultimately choose for reaching into your website's data cache will depend on which web engine you have chosen and which tools you employ.

So began my journey into the wonderful world of Javascript Object Notation, otherwise known as JSON.

For many website platforms, it might seem most direct to simply write a SQL to obtain the data needed on the backend database, used to store persistent data.  And on many websites, that is frequently done.

As for Blogger, the web monkey (Me) doesn't have access to a database per se.  The database, if there is one, isn't surfaced by Google.  Instead, what they expose is the aforementioned API, JSON.  A seemingly circuitous path, set of gymnastics a code monkey must contort into to get at the needed data.

Some things come more easily for dedicated programmers than the less initiated.  For starters, having a higher threshold of 'pain' works to one's advantage--when I can't find an answer, I obsess for days, gnash teeth, fidget, until I hit on the answer--pretty routine masochistic self-imposed torture.  Most programmers accept this as part of their occupation and resign themselves to pouring over on-line technical documentation, some better and some less so, all in a vengeful determined effort to divine the 'magic' to getting from point A (the problem) to point B (the solution)--returning the correct piece of data or desired function (behavior), hopefully.

So, after many starts and stops, interruptions, staring for long periods of time, ranging across the Internet (one time there was a sign 'You have reached the end of the Internet--turn around and go back'), I was finally feeling like I had decoded the 'Rosetta Stone' and came up with a subset of code which might be your 'prototypical' example for querying the JSON API:










Good Code Monkey.  Now press the button and take the pellet.  There.

Pretty much the above JSON code lives neatly inside of Javascript and co-exists nicely all wrapped up in a tidy HTML page.  One can also use just about any programming language under the Sun--there's most likely a library which encapsulates JSON for your preferred programming tool, be it PHP, Perl, Python, what have you.

Along the way, in my various readings, I came across this sterling article which summarizes quite well why JSON is revolutionizing the 'Internet of Things'.  The author Luc Perkins keenly observes:

"...Ten years ago, XML was the primary data interchange format. When it came on the scene, it was a breath of fresh air and a vast improvement over the truly appalling SGML (Standard Generalized Markup Language). It enabled people to do previously unthinkable things, like exchange Microsoft Office documents across HTTP connections. With all the dissatisfaction surrounding XML, it’s easy to forget just how crucial it was in the evolution of the web in its capacity as a “Swiss Army Knife of the internet.”

But it’s no secret that in the last few years, a bold transformation has been afoot in the world of data interchange. The more lightweight, bandwidth-non-intensive JSON (JavaScript Object Notation) has emerged not just as an alternative to XML, but rather as a potential full-blown successor. A variety of historical forces are now converging and conspiring to render XML less and less relevant and to crown JSON as the privileged data format of the global digital architecture of the future. I think that the only question is how near that future is.

I strongly believe that this transformation can be attributed to four broad trends, which I’ll discuss in turn:

1. APIs (application programming interfaces)
2. Big Data
3. The Internet of Things
4. Full-stack JavaScript..."

So, during my journey learning JSON, I have reached the conclusion that in today's 'mash-up' Web 2.0 world where one website incorporates chunks of data from another unbeknownst to the average user, behind the scenes is most likely JSON at work.

Ten years ago, XML might have been the only solution for serializing and exchanging data over http.
Even more sophisticated today is using JSON over Websockets which is only beginning to turn up in some of the more advanced website coding practices.

JSON's lightweight nature requires far less effort than XML and integrates into most website frameworks now as a plugin architecture if not present in its default installation.


So, does this fit with your view on the 'Internet of Things' and experience with the newest programming paradigms?  Please give your feedback!

-- Dietrich


END
Enhanced by Zemanta

2 comments:

  1. I love the JSON. I currently _do_ try and do my data transfers over web sockets, and it's beautiful. JSON is 'native' to JavaScript, and there's a built in encoding/decoding function in PHP. I know there is/are module/s for Python, and I suspect there are Perl modules, and Ruby implementations too.

    I like JSON because it's extremely simple, and relatively light. I find XML verbose, and error prone by comparison.

    ReplyDelete
  2. I come from the world of ANSI X12 EDI transaction sets and HL7. These have been my cross to bear for years and XML and JSON are, relatively-speaking mundane, but I completely agree--JSON is the ticket--elegant, simple and fits nicely into the Javascript ecosystem.

    ReplyDelete