JSON Data Types

JSON provides data for multiple uses in human-readable format and can be used by many programming languages like C++, C#, Python, Java, Perl, etc. So to satisfy their data structure and data type, JSON supports various data types of its own to make things easier. In this tutorial, you will be learning about the data types in JSON.

Data Types in JSON

JSON provides eight data types for dealing with different kinds of data.
  1. Number
  2. String
  3. Boolean
  4. Array
  5. Value
  6. Object
  7. Whitespace
  8. null

Number

Numbers provides floating point double precision data format and does not allow octal or hexadecimal number formats. Moreover, it does not assign NaN or infinity to its variables. The syntax will look something like this:

Syntax:
{ string : nunbervalue, .... }
Example:
var ob = {age: 64}

It also has three subcategories:

  1. Integer
  2. Fraction
  3. Exponent

String

A string is a double-quoted set of character(s) (Unicode) having backslash escaping. When you say character, it denotes a single character string value, having a string length of one. The syntax will look something like this:

Syntax:
{ string : "stringdata", ....}

Boolean

Boolean provides a boolean value of either true, i.e., 1 or false, i.e., 0. The syntax will look something like this:

Syntax:
{ string : true/false, .... }
Example:

{ name: 'Alex', age: 20, marks: false }

Array

An array is a collection of multiple values under one name and is enclosed within square braces as in other programming languages and separated by commas. But here the JSON users can start indexing its value from either 0 or 1. Array elements are used with their keys which are sequential integer values.
The syntax looks something like this:

Syntax:
[ value1, value2, ….]
Example:
{
  "department": [
    {
      "software_programming": "C++",
      "version": "17"
    },
    {
      "web_development": "HTML",
      "version": "5"
    },
    {
      "IT_management": "Entrepreneurship",
      "type": "startup"
    }
  ]
}

Value

Value provides data which can be of any data type among the following:

  • null
  • array
  • object
  • string
  • true or false
  • number etc

Object

The object provides an unordered collection of data in a name-value pairing format, and you can detect it easily because it keeps enclosed within a pair of curly brackets ({ and }). Between name and value, there is a colon (:) to separate these two entities. It is to be noted that the key/name must have to be a string. The syntax looks something like this:

Syntax:
{ string1 : value1, string2 : value2, ....}

Whitespace

Whitespace provides a gap for readability between tokens of JSON data. This can be done simply with the use of string and space within that double quotes, something like this:

Syntax:
{string:" ",...}

null

null provides a data which has nothing to it or other words; you can say it's variable is empty. Example:

Example:
var val_check = null;