Displaying articles with tag

Liquid error: undefined method `include?' for nil:NilClass

Posted by david, Wed Dec 26 21:15:00 UTC 2007

JSON has relatively few elements which are geared toward specific data types. The data types are inferred from the markup directly.

The types exposed via JSON are:

  • Data Types -
    • Numbers (4, 2E-12)
    • Boolean values (true and false)
    • Unicode strings
    • The nothing value, null
  • Structure Types -
    • Arrays
    • Objects; these are also known as a Dictionaries, or as Maps of String names to any defined type

This differs from XML, which really has only text content. Type in XML is external to the document, often specified by some schema document (DTD, XML Schema, and RelaxNG being the most common schema formats). The indirection in XML allows with greater flexibility in how data or other content is exposed; JSON by contrast often only has one logical way for the data to be structured, which leads to the 'schema' of the JSON document being exposed.

Like XML but unlike some of the binary data encodings, JSON does not have a binary data type. If you wish to transport binary data, like XML you will probably resort to base64 encoding the data for transmission

Liquid error: undefined method `include?' for nil:NilClass | Filed Under: | Tags:

Liquid error: undefined method `include?' for nil:NilClass

Posted by david, Wed Dec 26 20:51:00 UTC 2007

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: