JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Widely hailed as the successor to XML in the browser, JSON aspires to be nothing more than a simple, and elegant data format for the exchange of information between the browser and server; and in doing this simple task it will usher in the next version of the World Wide Web itself.

The Object: An Introduction

var myFirstObject = {};

The squiggly braces have the potential to record every bit of information humanity has ever gathered, and express the most complex programs computer scientists can dream up. In fact, Javascript itself is stored inside a set of squiggly braces just like that, as are all of its primitive data types -- strings, numbers, arrays, dates, regular expressions, they're all objects and they all started out just like myFirstObject.

Objects as Data

At its most base level a Javascript Object is a very flexible and robust data format expressed as name/value pairs. That is, an object holds a name which is an object's property -- think of it as a plain old variable name that's attached to the object name. And the object holds the value of that name.

var myFirstJSON = { "firstName" : "John",
                    "lastName" : "Doe",
                    "age" : 23 };
Object
An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
Array
An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
Value
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
String
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
Number
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.

This object has 3 properties or name/value pairs. The name is a string -- in our example, firstName, lastName, and age. The value can be any Javascript object (and remember everything in Javascript is an object so the value can be a string, number, array, function, even other Objects) -- In this example our values are John, Doe, and 23. John and Doe are strings but age is a number and as you can see this is not a problem.

var employees = { "accounting" : [ // accounting is an array in employees.
                                   { "firstName" : "John", // First element
                                     "lastName" : "Doe",
                                     "age" : 23 },
                                   
                                   { "firstName" : "Mary", // Second Element
                                     "lastName" : "Smith"
                                     "age" : 32 }
                                  ], // End "accounting" array.
                  "sales" : [ // Sales is another array in employees.
                                   { "firstName" : "Sally", // First Element
                                     "lastName" : "Green",
                                     "age" : 27 },
                                   
                                   { "firstName" : "Jim", // Second Element
                                     "lastName" : "Galley",
                                     "age" : 41 }
                                  ] // End "sales" Array
                } // End Employees

Here employees is an object. That object has two properties or name/value pairs. Accounting is an array which holds two JSON objects showing the names and age of 2 employees. Likewise sales is also an array which holds two JSON objects showing the name and ago of the two employees who work in sales. All of this data exists within the employees object. There are several different ways to access this data.

 

Accessing Data In JSON

The most common way to access JSON data is through dot notation. This is simply the object name followed by a period and then followed by the name/property you would like to access.

var myObject = { 'color' : 'blue' };

document.writeln(myObject.color); // outputs blue.

If your object contains an object then just add another period and name…

var myObject = { 'color' : 'blue',
                 'animal' : {'dog' : 'friendly' }
               };

document.writeln(myObject.animal.dog); // outputs friendly

The only limits to the structure are the amount of storage and memory available to it. Because JSON can store objects within objects within objects and arrays within arrays that can also store objects, there is no virtual limit to what a JSON object can store. Given enough memory and storage requirement, a simple JSON data structure can store, and properly index, all the information ever generated by humanity.

 

This is a title

Nam non augue quis augue convallis adipiscing. Integer id velit nunc. Fusce auctor luctus sollicitudin. Suspendisse et arcu metus. Sed hendrerit, neque vitae posuere lacinia, risus ipsum semper neque, et volutpat urna purus aliquet lectus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nam dignissim sagittis tellus sed hendrerit. Praesent porta fringilla nisi iaculis aliquet.

Aliquam fermentum feugiat enim, ut placerat neque dapibus accumsan. Sed vel turpis magna, non pulvinar quam. Morbi hendrerit, enim dignissim malesuada tempus, leo nisi ultricies ligula, sed porttitor tellus sem quis sapien. Fusce id porta quam. Phasellus convallis, magna at blandit euismod, massa metus malesuada sem, scelerisque rutrum orci lectus id neque. Maecenas varius dui eu enim venenatis bibendum. Aenean vel sapien tellus, ac porttitor nisl. Nullam vulputate nibh vel mi euismod ultrices. Aenean lobortis lacinia elit fermentum eleifend. In hac habitasse platea dictumst.