Python JSON Data Types














































Python JSON Data Types



       JSON  Data Types

 

JSON is most widelyused data format for data interchange on the web. It is alightweight text based, data-interchange format and it completely language independent. It is based on a subset of the JavaScript programming language and it is easy to understand and generate.


JSON supports mainly 6 data types:

  1.   string

  2.   number

  3.   boolean

4.   null


5.   object


6.   array


String: JSON strings must be written in double quotes like C-language there are various  

special characters(Escape Characters) in JSON that you can use in strings such as ( backslash),

 / (forward slash), b (backspace), n (new line), r (carriage return), t (horizontal tab) etc.

Example:

{ "name":"CppSecrets" }
{ "city":"Uttar Pradesh/India" }
here / is used for Escape Character / (forward slash).

Number: Represented in base 10 and octal and hexadecimal formats are not used.

Example:

{ "age": 20 }
{ "percentage": 82.44}


Boolean: This datatype can be either true or false.

Example:

{ "result" : true }

 Null: It is just a define nillable value.

Example:

{
"result" : true,
"grade" : null,
"rollno" : 210
}


Object: It is a set of name or value pairs inserted between {}
(curly braces). The keys must be strings and should be unique and multiple key
and value pairs are separated by a, (comma).

Syntax:

{ key : value, .......}


Example:

{
"CppSecrets":{ "name":"John", "age":20, "score": 99.05}
}


Array: It is an ordered collection of values and begins with [

(left bracket) and ends with ] (right bracket). The values of array are
separated by ,(comma).

Syntax:

[ value, .......]

Example:

{
"CppSecrets":[ "root", "roy", "stokes" ]
}

{
"collection" : [
{"id" : 101},
{"id" : 102},
{"id" : 103}
]
}

JSON Document:

{
"CppSecrets" : [
{
"name" : "root",
"subject" : "Python JSON",
"Articles" : 50
},
{
"name" : "Morgan",
"subject" : "Python Flask",
"Articles" : 25
},
{
"name" : "chahal",
"subject" : "Python Django",
"Articles" : 75
}
]
}




Comments