What are the Different Datatypes in JSON

We are all familiar with JSON but have you ever thought what are the datatypes used in the same? Check this article to learn more and get more clarity on the topic.

JSON supports the following 6 data types, the first 4 are primitive data types and the other 2 are complex types.

stringdouble-quoted Unicode characters of length 0 or more. Special characters need to be escaped with a backslash ().
numberany integer or floating point. JSON does not have separate types for them
booleantrue or false
nullnull or empty
arrayordered collection of values
objectan unordered collection of key/value pairs

Let’s see some more details on each of them.

string

  • It is a sequence of Unicode characters in double quotes. Its length can be 0 or more
  • Special characters are escaped with a backslash

Following are various special characters with backslash escaping.

NameCharacter
Backslash\
Double quote\”
Newline\n
Carriage return\r
Tab\t
Backspace\b
Forward slash\/
Form feed\f

Example

{
    "name": "John Davis",
    "address": "977 Crescent St.\nOld Bridge, NJ 08857",
    "forward": "Symbol for forward slash is /",
    "first_letter": "a",
    "error_msg": ""
}

number

  • A number can have an integer or floating-point value.
  • Both positive and negative values are supported.
  • e-notation is supported by having a value, integer or floating point, followed by e or E, then by the power of 10 to multiply by. For example, pi is 3.147e8

Example

{
    "num1": 60,
    "num2": -35,
    "num3": 0.5,
    "num4": -133.234,
    "pi": 314700000
}

boolean

  • boolean values are simply true or false.
  • booleans are not enclosed in quotes.

Example

{
    "participated": true,
    "cleared": false
}

null

  • null means empty. When a value is not found then it is represented as null.
  • null is special in JSON, it shows that the value is not there.
  • null does not need to be enclosed in quotes.

Example

{
    "max_score": 70,
    "avg_score": null
}

array

  • an array is used to store an ordered collection of values.
  • an array can have 0 or more values.
  • array values can be of different data types.
  • array values are comma-separated and enclosed in square brackets i.e. []
  • an array can also contain objects.

Example

{
  "names": ["John", "Michael", "Lewis", "Jordan"],
  "ids": [101, 102, 203, 404],
  "random": ["John", 45, null, false, 3.4],
  "allowed_cards": []
}

Objects in an array

{
  "candidates": [
    {
      "first_name": "John",
      "cleared": true
    },
    {
      "first_name": "Jordan",
      "cleared": false
    },
    {
      "first_name": "Davis",
      "cleared": true
    }
  ]
}

object

  • an object is an unordered collection of key/value pairs.
  • objects are separated by commas and enclosed in curly braces.
  • An object can have 0 or more values.
  • The values in an object are allowed to have different data types.
  • an object can contain an array.

Example

{
    "first_name": "John",
    "cleared": true,
    "age": 45,
    "address": {
        "flat_no": 977,
        "street": "Crescent St.",
        "state": "NJ"
    },
    "allowed_cards": [
        "American Express",
        "Visa"
    ]
}