JSON Data Types

At the granular level, JSON consists of 6 data types.

The first four data types (string, number, boolean and null) can be referred to as simple data types. The other two data types (object and array) can be referred to as complex data types.

  1. string
  2. number
  3. boolean
  4. null/empty
  5. object
  6. array

Let’s learn about each data type one by one.

1. String

A string is a sequence of zero or more Unicode characters, enclosed between " and " (double quotes). Strings wrapped in single quotes ' are not valid.

{
    "color" : "Purple"
}

JSON Strings can contain the following backslash-escaped characters:

  • \" – Double quote
  • \\ – Backslash
  • \/ – Forward slash
  • \b – Backspace
  • \f – Form feed
  • \n – Newline
  • \r – Carriage return
  • \t – Tab
  • \u – Trailed by four hex digits

2. Number

JSON numbers follow JavaScript’s double-precision floating-point format.

  1. Represented in base 10 with no superfluous leading zeros (e.g. 67, 1, 100).
  2. Include digits between 0 and 9.
  3. Can be a negative number (e.g. -10.
  4. Can be a fraction (e.g. .5).
  5. Can also have an exponent of 10, prefixed by e or E with a plus or minus sign to indicate positive or negative exponentiation.
  6. Octal and hexadecimal formats are not supported.
  7. Can not have a value of NaN (Not A Number) or Infinity.
{
	"number_1" : 210,
	"number_2" : -210,
	"number_3" : 21.05,
	"number_4" : 1.0E+2
}

3. Boolean

Booleans values can be either true or false. Boolean values are not surrounded by quotes and will be treated as string values.

{
	"visibility" : true
}

4. null

Although technically not a value Type, null is a special value in JSON. When there is no value to assign to a key, it can be treated as null.

null value should not be surrounded by quotes.

{
	"visibility" : true,
	"popularity" : null, //empty
	"id" : 210
}

5. Object

  1. An unordered set of name/value pairs inserted between {} (curly braces).
  2. An object can contain zero or more name/value pairs.
  3. Multiple name/value pairs are separated by a , (comma).
{
	"visibility" : true,
	"popularity" : "immense",
	"id" : 210
}

6. Array

  1. An ordered collection of values.
  2. Begins with [ (left bracket) and ends with ] (right bracket).
  3. It’s values are separated by , (comma).
{
	"ids" : ["1","2","3"]
}

//or

{
"ids" : [
		{"id" : 1},
		{"id" : 2},
		{"id" : 3}
	]
}

Was this article helpful?

Comments

  1. The null value cannot be represented by an empty value (https://tools.ietf.org/html/rfc7159). It must be represented by “null” without the quotation marks. The definition of string is wrong (for example, certain characters must be “escaped” within a string). In general, JSON has a specific syntax that must be followed to allow it to interchange data universally.

    Of course, local data storage not intended for universality is free to modify JSON syntax in any way desired, in which case the format is no longer to be called JSON. Examples include including comments for configuration files and rules for omitting quotation marks around certain property keywords and/or string values.

    Reply

Leave a Comment