Blog

JSON: What It Is, How It Works, & How to Use It

What is JSON?

  • JSON is stand for JavaScript Object Notation. and an open standard format that uses human-readable text to transmit data objects consisting of attribute`value pairs.
  • JSON is a syntax for storing and exchanging data.
  • JSON is an easier-to-use alternative to XML and it is a standard way to store data/information in an organized, easy-to-access manner.
  • it gives us a human-readable collection of data that we can access in a really logical manner.
  • JSON is a lightweight data-interchange format.
  • JSON is language independent.
  • JSON is “self-describing” and easy to understand.

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.
Note: JSON uses JavaScript syntax, but the JSON format is text only, just like XML.
Text can be read and used as a data format by any programming language.

Image result for json

History of JSON:

Douglas Crockford was the first to specify and popularize the JSON format.The acronym was coined at State Software, a company co-founded by Crockford. JSON grew out of a need for stateful, real-time server-to-browser communication without using browser plugins such as Flash or Java applets, which were the dominant methods in the early 2000s and 2001.

Image result for json

JSON Syntax:

The JSON syntax is a subset of the JavaScript syntax.

JSON Syntax Rules:

JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

JSON Data – A Name and a Value:

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:

eg: “firstName”:“John”

Note:JSON names require double quotes. JavaScript names don't.

JSON Values:

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:

JSON objects are written inside curly braces.

Just like JavaScript, JSON objects can contain multiple name/values pairs:

eg: {"firstName":"John", "lastName":"Doe"}

JSON Arrays:

JSON arrays are written inside square brackets.

Just like JavaScript, a JSON array can contain multiple objects:

eg: “customers”:[
{“firstName”:“John”, “lastName”:“Doe”},
{“firstName”:“Anna”, “lastName”:“Smith”},
{“firstName”:“Peter”,“lastName”:“Jones”}
]

 

JSON Uses JavaScript Syntax:

Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript.

With JavaScript you can create an array of objects and assign data to it, like this:

eg: var employees = [
 {"firstName":"John", "lastName":"Doe"},
  {"firstName":"Anna", "lastName":"Smith"},
  {"firstName":"Peter","lastName": "Jones"}
 ];

What are the advantages and disadvantages of json vs xml for ajax requests?

  1. JSON is smaller than corresponding XML
  2. JSON is faster, i.e. simpler syntax -> easier parsing (faster parsing)
  3. JSON can be mapped more easily into object oriented system.
  4. JSON is smaller, faster and lightweight compared to XML. So for data
    delivery between servers and browsers, JSON is a better choice.
  5. JSON and XML both use Unicode and thus supports
    internationalization.

Example of JSON:

{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}

 

 

Exmaple of XML:

<!DOCTYPE glossary PUBLIC "-//OASIS//DTD DocBook V3.1//EN">
 <glossary><title>example glossary</title>
  <GlossDiv><title>S</title>
   <GlossList>
    <GlossEntry ID="SGML" SortAs="SGML">
     <GlossTerm>Standard Generalized Markup Language</GlossTerm>
     <Acronym>SGML</Acronym>
     <Abbrev>ISO 8879:1986</Abbrev>
     <GlossDef>
      <para>A meta-markup language, used to create markup
languages such as DocBook.</para>
      <GlossSeeAlso OtherTerm="GML">
      <GlossSeeAlso OtherTerm="XML">
     </GlossDef>
     <GlossSee OtherTerm="markup">
    </GlossEntry>
   </GlossList>
  </GlossDiv>
 </glossary>



Awadhesh Kumar
Jain Software Dvelopers