JSON

I thought the Wikipedia definition of JSON gave a good description so thought I would quote its introduction. The official pages that go into much more detail can be found at www.json.org.

JSON or JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data between a server and web application, as an alternative to XML.

Although originally derived from the JavaScript scripting language, JSON is a language-independent data format, and code for parsing and generating JSON data is readily available in a large variety of programming languages.

The key take away points are;

  • JSON is a syntax for storing and exchanging data
  • JSON is an easier to use alternative to XML
  • JSON lightweight data interchange format
  • JSON is language independent
  • JSON is “self-describing” and easy to understand

Much Like XML;

  • Both JSON and XML is plain text
  • Both JSON and XML is “self-describing” (human readable)
  • Both JSON and XML is hierarchical (values within values)
  • Both JSON and XML can be fetched with an HttpRequest

Much Unlike XML

  • JSON doesn’t use end tag
  • JSON is shorter
  • JSON is quicker to read and write
  • JSON can use arrays

JSON Syntax

JSON syntax is a subset of the JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays
  • JSON Name/Value Pairs
  • JSON data is written as name/value pairs.

A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value:

    "firstName":"Rob"

JSON values can be:

  • A number (integer or floating point)
  • A string (in double quotes)
  • A Boolean (true or false)
  • An array (in square brackets)
  • An object (in curly braces)
  • null

JSON objects are written inside curly braces. Objects can contain multiple name/values pairs:

    {"firstName":"John", "lastName":"Smith"}

JSON arrays are written inside square brackets. Just like in JavaScript, an array can contain multiple objects:

    { "contacts":[ {"firstName":"John", "lastName":"Smith"}, 
    {"firstName":"Fred", "lastName":"Blogs"},
    {"firstName":"Rob", "lastName":"Edwards"} ] }

In the example above, the object “contacts” is an array containing three objects. Each object is a record of a person (with a first name and a last name).

To compare against the equivalent XML format;

    <contacts> <contacts>
    <firstName>John</firstName>
    <lastName>Smith</lastName> </contacts>
    <contacts> <firstName>Fred</firstName>
    <lastName>Blogs</lastName> </contacts>
    <contacts> <firstName>Rob</firstName>
    <lastName>Edwards</lastName> </contacts>
    </contacts>

JSON Files

The file type for JSON files is “.json”
The MIME type for JSON text is “application/json”

Show Comments

Get the latest posts delivered right to your inbox.