I have had a few people who have looked at my blog now, and the majority of them have asked me “what is JSON anyway?” There are some good places to learn more without having to resort to reading an RFC, but I’ll take a shot at explaining it in my own way here.
JSON is short for JavaScript Object Notation. You are not required to use, know, or understand JavaScript (or rather, ECMAScript) in order to leverage JSON, however. It is instead used as a markup for representing structured data. This markup just happened to grow of more importance out of the “Web 2.0 / AJAX” phenomenon, because so much more code was written in browser script. The significance is that the syntax is interpretable by the scripting language directly – rather than using an XML api to load and manipulate a document into an internal data representation, you can simply do an ‘eval’ to have a new object created from the data supplied.
The competition in markup formats between JSON and XML comes up often. Both have their places – while JSON is good for representing simple structured data, XML is much better for semantic markup of text – things like HTML documents. A lot of the benefits of JSON come from it being ‘simpler’, both in use and in required body of reading to learn.
Here however is an example based on the DIGG api (adapted from http://apidoc.digg.com/ListGalleryPhotos, but shortened for length.):
- In XML:
-
<gallery timestamp="1193358475" min_date="1190766450" total="31794" offset="0" count="3">
<galleryphoto id="3920948" submit_date="1193358472" comments="0"
src="http://digg.com/users/KalimaSaraswati/gallery/3920948/t.jpg"
href="http://digg.com/users/KalimaSaraswati/gallery/3920948">
<title>241_4171 Sun Halo Upper Tangent Arc.jpg</title>
<user name="KalimaSaraswati" icon="http://digg.com/img/udl.png"
registered="1193358174" profileviews="5" />
</galleryphoto>
</gallery>
- In JSON:
-
{
"timestamp" : 1193358478,
"min_date" : 1190766450,
"total" : "31794",
"offset" : 0,
"photos" : [
{
"id" : 3920948,
"submit_date" : 1193358472,
"comments" : 0,
"title" : "241_4171 Sun Halo Upper Tangent Arc.jpg",
"user" : {
"name" : "KalimaSaraswati",
"icon" : "http://digg.com/img/udl.png",
"registered" : 1193358174,
"profileviews" : 5
},
"src" : "http://digg.com/users/KalimaSaraswati/gallery/3920948/t.jpg",
"href" : "http://digg.com/users/KalimaSaraswati/gallery/3920948"
}
]
}
Liquid error: undefined method `include?' for nil:NilClass |
Filed Under: |
Tags: tutorials