Blog

categories

tags

JSON

JSON (JavaScript Object Notation) is a data format commonly used for transmitting information over the web. Structurally, it is similar to XML, in that it uses nesting and hierarchies to cluster related pieces of information. However, because it has a “lighter” (less verbose) punctuation scheme than XML and its organization is based on existing JavaScript conventions, its has become largely dominant on the web. Many applications now offer data in JSON as well as XML.

What is it good for?
While JSON is essentially human-readable, its lack of closing tags can make it somewhat more challenging to write when compared to XML. The faster processing provided by the smaller file size and javascript-friendly structure, however, make it an increasingly dominant form of data transmission.

What does it look like?
Below is an example of the same document, first XML, then in JSON, from JSON.org.

XML:

<menu id="file" value="File">
 <popup>
       <menuItem value="New" onClick="CreateNewDoc()" />
       <menuItem value="Open" onClick="OpenDoc()" />
       <menuItem value="Close" onClick="CloseDoc()" />
 </popup>
</menu>

JSON:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

Note that .json files have no document type declaration.

How do I use it?
As noted above, JSON syntax follows a pattern broadly similar to XML, with different “punctuation” and an additional grammatical structure:
1. Elements (or “objects”) in JSON are opened and closed using only curly braces ({}). See “menu” tag.
2. Attributes in JSON appear as “name/value” pairs, with both the name and value surrounded by double quotation marks and separated by a colon (:). Multiple attributes are separated by commas instead of whitespace. See “menu” tag.
3. Lists (or “arrays”). In XML, creating multiple elements at the same level with the same name acts as a list of elements. In JSON, the element name is declared only once, and the list items are surrounded by square brackets ([]). See “menuitem” tag.

Where can I find it?
Because browsers will not generally display JSON, if you follow a link to a .json file (typically via an API), you will generally be prompted to download it. Save the file with a .json extension and then open it in NotePad or another text editor.

Issues & FAQs
My JSON is all jammed up on one line. Now what?
Web developers to the rescue! Thanks to the efforts of Arc90 and others, we all have JSONLint. It will make good JSON pretty and shout at you if (and where) something is wrong.

Tools & Tutorials
JSONLint: Will vet your JSON and format it up for you.