Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSON data type


May 08, 2021 JSON


Table of contents


JSON data type

The JSON format supports the following data types:

Type Describe
Digital (Number) Double floating-point format in JavaScript
String type Double quote-wrapped Unicode characters and backslash escape characters
Boolean true or false
Array An ordered sequence of values
Value It can be strings, numbers, true or false, null, and so on
Object Out-of-order keys: Value pairs collection
Whitespace Can be used between any pair of symbols
Null Empty

Digital

  • The double floating-point format in JavaScript depends on the implementation.
  • Octal and heteen formats cannot be used.
  • NaN and Infinity cannot be used in numbers.

The following table shows the types of numbers:

Type Describe
Shaping (Integer) Numbers 1-9, 0 and plus or minus
Score (Fraction) Scores, such as .3, .9
Index (Exponent) Indices, e.g. e, e, e-, E, E, E-

Grammar:

var json-object-name = { string : number_value, .......}

Example:

The following example shows a numeric type whose value should not be wrapped in quotation marks:

var obj = {marks: 97}

String type

  • Zero or more double quote-wrapped Unicode characters and backslash escape sequences.
  • A character is a string with only one character and a length of 1.

The following table shows the string types:

Type Describe
" Double quotes
\ Backslash
/ Slash
B The back-checker
F Page break
N Line breaks
R Carriage return
T Horizontal tabs
u Four heteens

Grammar:

var json-object-name = { string : "string value", .......}

Example:

The following example shows the string data type:

var obj = {name: 'Amit'}

Boolean type

It contains both true and false values.

Grammar:

var json-object-name = { string : true/false, .......}

Example:

var obj = {name: 'Amit', marks: 97, distinction: true}

Array

  • It is an ordered collection of values.
  • Close with square brackets, which means that the array ends with .
  • The value is split using , (comma).
  • The array index can start with 0 or 1.
  • Arrays should be used when the key name is a continuous integer.

Grammar:

[ value, .......]

Example:

The following example shows an array of multiple objects:

{
    "books": [
        { "language":"Java" , "edition":"second" },
        { "language":"C++" , "lastName":"fifth" },
        { "language":"C" , "lastName":"third" }
    ]
}

Object

  • It is a disordered collection of name/value pairs.
  • The object is closed with braces, starting with ''' and ending with ''.
  • Each name is followed by a ':' (colon), and the first/value pair is split using , (comma).
  • The key name must be a string and cannot have the same name.
  • Objects should be used when the key name is any string.

Grammar:

{ string : value, .......}

Example:

The following example shows the object:

{
    "id": "011A",
    "language": "JAVA",
    "price": 500,
}

Space

You can insert between any pair of symbols. Y ou can add to make your code more readable. The following example shows a declaration that uses spaces and does not:

Grammar:

{string:"   ",....}

Example:

var i= "   sachin";
var j = "  saurav"

Null

Means empty type.

Grammar:

null

Example:

var i = null;

if(i==1) {
    document.write("<h1>value is 1</h1>");  
} else {
    document.write("<h1>value is null</h1>");
}

JSON value

Including:

  • Numbers (integer and floating-point)
  • String
  • Boolean value
  • Array
  • Object
  • Null

Grammar:

String | Number | Object | Array | TRUE | FALSE | NULL

Example:

var i =1;
var j = "sachin";
var k = null;