An Introduction to JSON format

This article is based on basics of JSON, a model for the representation of information widely used by Web applications that work with AJAX technology and many more.

 1. Introduction to JSON

JSON (JavaScript Object Notation) is a model for storage and transmission of information in text format. Although very simple, it has been widely used by Web applications due to its ability to structure information in a form much more compact than that achieved by the XML model, making it faster parsing such information. This explains the fact that the JSON have been adopted by companies such as Google and Yahoo, whose applications need to transmit large volumes of data.

This article provides an introduction to JSON format and is divided as follows. Section 2 describes the syntax of the language. Then, the section 3 performs a comparison between XML and JSON models. 

2. Syntax

The idea used by JSON to represent information is tremendously simple: for each value represented, is assigned a name (or label) that describes its meaning. This syntax is derived from the form used by JavaScript to represent information. For example, to represent the year 2012, we use the following syntax:

Listing 1: Representing The Year 2016 in JSON

A name / value pair must be represented by name in double quotation marks, followed by a colon, followed by the value. Values can only have 3 basic types : numeric (integer or real), boolean and string. In Listings 2, 3, 4 and 5 are the example for the same. Note that the values of type string should be represented in quotes.

Listing 2: Representing a Real Number in JSON

Listing 3: Representing a String in JSON

Listing 4: Representing a Negative Number in JSON

Listing 5: Representing a Boolean Value in JSON

From the basic types, you can build complex types : array and object. The arrays are enclosed in square brackets, with its separate elements between commas. The listings 6 and 7 show examples for the same.

Listing 6: Array of Strings

Listing 7: Array of Integers

The objects are specified and braces may be composed of multiple name / value pairs for arrays and also by other objects. In this way, a JSON object can represent virtually any kind of information! The example in Listing 8 shows the data representation of a Movie.

Listing 8: Object

You can represent more than one object or record at a time. An example is shown in Listing 9, wherein two films are represented in an array.

Listing 9: Array objects

Finally, it is important to mention that the keyword “null” should be used for the representation of null values (Listing 10).

Listing 10: Representing a null value

3. JSON Format & XML Format

We can understand the JSON as a kind of “competitor” of XML in information exchange area. In this section, we present some of the main similarities and differences between the two models for the representation of information.

Similarities:

The two models represent information in text format.
Both have self-descriptive (ie, just “hit the eye” in a JSON file or an XML file to understand its meaning).
Both are capable of representing complex information, difficult to represent in tabular format. Some examples: compound objects (objects within objects), hierarchical relations, multivalued attributes, arrays, missing data, etc.
Both can be used to carry information AJAX applications.
Both patterns can be considered for data representation. XML is a W3C standard, while JSON was formalized in RFC 4627.
Both are language independent. Data represented in XML and JSON can be accessed by any programming language through specific API’s.

Differences:

JSON is not a markup language. It has no opening tag and much less lock!
The information represented in JSON can not be validated by a schema. Already in XML, you can validate using a DTD and XML Schema.
JSON is the information more compactly.
JSON does not allow the execution of processing instructions, something possible in XML.
JSON is typically designed to exchange information while XML has more applications. For example, these days there are entire databases stored in XML and structured DBMS’s native XML.

 

For More Details about JSON go to http://www.json.org/

Leave a Reply

Your email address will not be published. Required fields are marked *