Learnitweb

JSON Data Types

JSON data types are:

  • Null
  • Number
  • String
  • Boolean
  • Array
  • Object

Null

  • Null is used to indicate that a data value does not exist, it is missing or that we do not know.
  • A lack of value is not the same thing as a value of zero or empty string.
  • Null is also not the same as undefined in JavaScript.
  • In JSON null is always in lowercase.
{
	"vehicle":"car",
	"registrationNo":null
}

Number

  • A number in JSON can be an integer, float or decimal or exponent.
  • A number can also have a negative value.
  • Examples of number are marks in a subject, amount, latitude/longitude etc.
{
	"salary":12000,
	"previous_Balance":-1305.50,
	"earthsMass": 5.97219e+24
}

String

  • In JSON string is comprised of Unicode characters.
  • A string value must always be enclosed in double quotes.
  • If you want to include double quotes (“) in the String in JSON, use backslash (\) to escape double quote.
  • This backslash character will tell the parser that the quote is not the end of the string.
  • If you need to escape backslash then you have to use backslash (\).

Following are the characters which need to be escaped with backslash in JSON string.

  • ” (double quotes)
  • \ (backward slash)
  • \/ (forward slash)
  • \b (backspace)
  • \f (form feed)
  • \t (tab)
  • \n (new line)
  • \r (carriage return)
  • \u followed by hexadecimal characters
{
	"message1":"hello world",
	"messag2":"hello world message from \"Learnitweb\"",
	"message3":"hello world message from \\t Learnitweb",
	"message4":"c:\\Program Files"
}

Boolean

  • In JSON, the literal value for the boolean data type is true or false.
  • Please note that in JSON true and false are always in lowercase.
{
	"isValid":true,
	"isFirstDay":false
}

Array

  • For the sake of simplicity, array can be considered as a collection of elements.
  • The array is surrounded by square brackets ([]).
  • Array elements are separated by comma (,).
  • The array elements can be any valid JSON data type (string, number, object, boolean, array and null).
{
	"days":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
	"list":[1,2,3,4,5,6,7,8,9]
}

Object

An Object is a name-value pair surrounded by curly braces ({}).

{
	"name":"John",
	"city":"London"
}

The value of a name in name-value pair can itself be an Object. For example:

{
	"person": {
		"name":"John",
		"city":"London"
	}
}