JSON (JavaScript Object Notation) is a,
·
Lightweight, an open standard data exchange
format
·
Easy to read and write.
·
Easy for machines to parse and generate
·
Based on a subset of the JavaScript
Programming Language
·
Completely language independent but uses
conventions that are familiar in the C-family of languages
·
Format was originally specified by Douglas
Crockford
Things of Note,
·
JSON is limited to text and numeric values.
Binary values are not supported
·
Because of a subset of the JavaScript
Specification. Therefore directly supported in JavaScript
·
Use primarily to transmit data between a
server and web application, as an alternative to XML
·
The JSON filename extension is .json
Data structures in JSON are based on key / value pairs which
starts with "{" and ends with "}". The key is a string, the
value can be a numerical value, a Boolean value (true or false) or an object.
e.g.:- {
"collection" : {
"title" : "Blog",
"description" : "This is a
description of my blog.",
"categories" : [
"Category-1", "Category-2" ]
}
}
·
Key: A key is always a string enclosed in
quotation marks.
·
Value: A value can be a string, number,
Boolean expression, array, or object.
·
Key/Value Pair: A key value pair follows a
specific syntax, with the key followed by a colon followed by the value.
Key/value pairs are comma separated.
e.g.:- "title” : "Blog"
This example is a key/value pair. The key is "title" and
the value is "Blog".
Types of Values
·
Array: An associative array of values.
·
Boolean: True or false.
·
Number: An integer.
·
Object: An associative array of key/value
pairs.
·
String: Several plain text characters
which usually form a word.
Arrays
e.g.: -
"collection"
: {
"title" : "Blog",
"categories" : [ "Category-1",
"Category-2" ]
}
Objects
An object is indicated by curly brackets. Everything inside of the
curly brackets is part of the object. We already learned a value can be an
object. So that means "collection" and the corresponding object are a
key/value pair.
e.g.: -
"collection"
: {
"title" : "Blog"
}
JSON generally ignores any whitespace around or between syntactic
elements (values and punctuation, but not within a string value). However JSON
only recognizes four specific whitespace characters: the space, horizontal tab,
line feed, and carriage return. JSON does not provide or allow any sort of
comment syntax.
The following example shows a possible JSON representation
describing a person.
{
"firstName" :
"Saman",
"lastName" :
"Silva",
"isAlive" : true,
"age" : 25,
"height_cm" :
167.6,
"address" : {
"streetAddress" :
"21 2nd Street",
"city" :
"Kandy"
},
"phoneNumbers" :
[
{
"type" :
"home",
"number" :
"12345678"
},
{
"type" :
"office",
"number" :
"76544332"
}
"children" : [],
"spouse" : null
}
Advantages of JSON
·
Simplicity, simpler than XML. JSON has a much
smaller grammar and maps more directly onto the data structures used in modern
programming languages
·
Openness, JSON is at least as open as XML
·
JSON is much easier for human to read than XML.
It is easier to write, too. It is also easier for machines to read and write
·
Can be used as an exchange format to enable
users to move their data between similar applications
·
Provides a structure to data so that it is
richer in information
·
JSON, being a simpler notation, needs much less
specialized software. In the languages JavaScript and Python, the JSON notation
is built into the programming language. In other languages, only a small amount
of JSON-specific code is necessary
·
JSON is a better data exchange format. XML is a
better document exchange format
·
Self-Describing Data
·
XML and JSON both use Unicode
·
JSON is data-oriented. JSON can be mapped more
easily to object-oriented systems
Applications
·
JSON-RPC
JSON-RPC is an RPC (remote
procedure call) protocol built on JSON, as a replacement for XML-RPC or
SOAP (Simple Object Access
protocol). It is a simple protocol that defines only a handful of data
types and commands. JSON-RPC lets a system send notifications (information to
the server that does not require a response) and multiple calls to the server
that can be answered out of order.
·
Ajax
JSON is often used in Ajax (Asynchronous JavaScript and XML)
techniques. Ajax is a term for the ability of a webpage to request new data
after it has loaded into the web browser, usually in response to user actions
on the displayed webpage. As part of the Ajax model, the new data is usually
incorporated into the user interface display dynamically the moment it arrives
back from the server. For example when the user is typing into a search box,
client-side code sends what they type to a server that will respond with a
possible list of items from its database. These might be displayed in a
drop-down list beneath the search box. The user may then stop typing and select
the relevant string directly. Now many developers use JSON to pass the Ajax
updates between the server and the client.
References
·
http://en.wikipedia.org/wiki/JSON