Yesterday a reader asked me a question about one of my blog posts. In the blog post, I described a Cordova application that would retrieve a list of URLs from a server. It would then fetch each URL and download the resource to the device. I casually described the data as "an array of URLs in JSON" and assumed everyone would know what that meant. Of course, not everyone does know what JSON means.
It got me thinking about how someone would learn more about JSON and become more familiar with it. You can start over at json.org for a formal definition, but I found the Wikipedia page, specifically the syntax example here, to be a bit simpler to digest.
At it's heart, JSON is a string format that can represent data. Just like XML, it can take abstract data structures and represent them in a string format that can be sent over the wire. That makes it specifically well suited for APIs. JavaScript has native support for working with it as do loads of other languages (including ColdFusion). Compared to XML, JSON is much slimmer and just as easy to read.
JSON represents simple values, like strings and numbers, just as they are, so "ray" in JSON is... "ray", and 6 is 6.
Arrays are represented by using brackets and a comma between each item: ["ray", 6, "i am not a number", "etc"]
Objects (ColdFusion folks - think structures) are represented by curly brackets. Each name/value pair is represented by name, colon, value. So: { name:"Ray", age:41, gender:"awesome"}.
Working with JSON in JavaScript is relatively trivial. If you use jQuery to make an XHR request then you can simply tell jQuery that a URL returns JSON and you've get a native JavaScript object to work with. Modern browsers also ship with support to create JSON (stringify) and read it (parse). You can read more about that at the excellent MDN docs: JSON.
Knowing the function that creates JSON, a nice way to play with it is to open your browser console and just try a few things out.
Any other suggestions? On a whim, I asked on Twitter how folks learned or were introduced to JSON. I got some fun responses (and hopefully this works, the preview doesn't show replies, but they should be showing up):
@raymondcamden Coming from JavaScript, when I first saw JSON, I was like, “Oh, it’s JavaScript” :) Which, it’s not. But, made if familiar.
— Ben Nadel (@BenNadel) January 29, 2015
@raymondcamden first heard about JSON at CF meetup in Phx @nathanstrutz (I think). Took some time for comfort, just over time with use.
— Brandon Moser (@brandonmoser) January 29, 2015
@raymondcamden same as @bennadel. Came from a Javascript bg, so it was easy to understand the structure. JSON looks close to js ojects.
— Sean Thompson (@ImSeanThompson) January 29, 2015
@raymondcamden When it was added to the Adobe runtimes, of course ;)
— Joseph Labrecque (@JosephLabrecque) January 29, 2015
@raymondcamden I adopted JSON shortly after learning of it. It just made sense to xfer data in similar syntax to the objects I was coding.
— Eric Leonard (@TheHeretical) January 29, 2015
@raymondcamden Learned about JSON from REST interfaces. Loved it right away for its terseness and ease of use.
— Robert Munn (@robertdmunn) January 29, 2015
@raymondcamden RE: JSON, I was working with a new API and it was like love at first sight. So much easier and faster than XML
— Jon Clausen (@jclausen) January 29, 2015