Home > DeveloperSection > Articles > Data Types in JSON

Data Types in JSON


JSON JSON 
Ratings:
0 Comment(s)
 2814  View(s)
Rate this:

Data Types in JSON

 

In this article I am trying to explain the concept of Data Types in JSON.

We already have read about the JSON in previous blog with following link.

http://www.mindstick.com/Blog/304/Introduction%20to%20JSON

JSON is smaller than XML, and faster and easier to parse. JSON (an acronym for JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript programming language for representing simple data structures and associative arrays, called objects. JSON is syntax for storing and exchanging text information.

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.

In JSON every variable and object must have a declared type, following datatypes supported by JSON format:

 

Number

It is a dual accuracy floating-point format in JavaScript and it depends on implementation. In JSON octal and hexadecimal formats are not used. But No NaN or Infinity is used in Number.

SYNTAX:

var json_obj_int_name = { string : number_value, ...}

EXAMPLE:

Example showing Number Datatype, value should not be quoted:

var obj = {age: 78}

String

String is order of double quoted Unicode characters with backslash escaping. Character is a single character string i.e. a string with length 1.

Below table is shows the string types:

Type

Description

"

 double quotation

\

 reverse solidus

/

 solidus

b

 backspace

f

 form feed

n

 new line

r

 carriage return

t

 horizontal tab

u

 four hexadecimal digits

SYNTAX:

var json_obj_string_name = { string : "string value", ...}

EXAMPLE:

var _string_obj = {name: 'Vijay'}

Boolean

It contain only true or false values.

SYNTAX:

var json_bool_obj= { string : true/false, .......}

EXAMPLE:

var obj = {isActive: true}

Array

Array data type is a methodical assortment of values. These are bounded with square brackets which means that array begins with '[' and ends with ']' and values are separated with ‘,' (comma). Array indexing can be started at 0 or 1.

SYNTAX:

[Value, ….]

EXAMPLE:

Below example showing array containing multiple objects:

{

  "New Delhi": [

   {"State":" National Capital Territory of Delhi ", "Country":"India"},

   {"Established ":" 1911 ", " Area":" 42.7 km (16.5 sq mi) "},

   {" Elevation":" 216 m (709 ft) "," Time zone ":" IST (UTC+5:30) "}

  ]

}

Object

It is an unordered set of name/value pairs.

Object are bounded in curly braces that is it starts with '{' and ends with '}'.

Each name is followed by ':'( colon) and the name/value pairs are separated by ',' (comma).

The keys must be strings and should be different from each other.

SYNTAX:

{ string : value, ...}

EXAMPLE:

Example showing Object:

{

 "Name": "Vijay",

 "Contact": "+91-1248554758",

 "Age": 24,

}


Don't want to miss updates? Please click the below button!

Follow MindStick