JSON Data Type


JSON is a data format that is based on JavaScript object syntax. It is commonly used to transmit data between a server and a web application.

In this article, you will learn about the different data types supported by JSON.


Valid Data Types

In JSON, data is represented as key-value pairs. The keys are strings, and the values can be one of the following types:

Warning! JSON does not support any other data type. Like:

  • Function
  • Date
  • Complex number
  • Any other data type not listed above

1. JSON Number

JSON supports numbers in the following formats:

Here are some examples of valid JSON numbers:

{
  "age": 25,
  "salary": 25000.00,
  "tax": 2.5e+3
}

2. JSON String

JSON strings must always be enclosed in double quotes. Here are some examples of valid JSON strings:

{
  "name": "John",
  "address": "123, Main Street, New York",
  "phone": "123-456-7890"
}

Warning! Single quotes are not allowed in JSON strings.


3. JSON Boolean

JSON supports two boolean values: true and false.

It is important to note that the boolean values must be in lowercase and not enclosed in quotes.

{
  "isAlive": true,
  "isDead": false
}

4. JSON Null

JSON supports a special value called null. It is used to represent a null value.

Just like boolean values, null should also be in lowercase and not enclosed in quotes.

{
  "name": null
}

5. JSON Object

JSON objects are used to represent a collection of key-value pairs. The keys are strings, and the values can be any of the valid JSON data types.

You can also nest objects within objects.

{
  "name": "John",
  "age": 25,
  "address": {
    "street": "123, Main Street",
    "city": "New York",
    "state": "NY"
  }
}

6. JSON Array

JSON arrays are used to represent an ordered list of values. The values can be any of the valid JSON data types.

An array starts with [ and ends with ]. The values are separated by commas.

{
  "name": "John",
  "age": 25,
  "phone": [
    "123-456-7890",
    "234-567-8901"
  ]
}

Conclusion

In this article, you have learned about the different data types supported by JSON.

The next article in this series will teach you how to use JSON in JavaScript.