Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

json

What data types are supported by JSON?

Amaaz Ahmad

JSON stands for Javascript Object Notation. It is a simple text-based format that is used to communicate or store data over the web.

A JSON object is enclosed in curly brackets, i.e., {}.

JSON supports six data types, which are divided into two classes. These classes are:

  • Simple/Primitive Data Types
  • Complex Data Types

Four of the supported data types are primitive data types. These are:

  • String
  • Number
  • Boolean
  • Null

The remaining two data types are complex. These are:

  • Object
  • Array

Each data type is briefly described with an example below.

String

The string data type stores text-based data. A string is enclosed within double quotation marks, i.e., " ".

For example, the following snippet of code contains a JSON object with a property called company_name, and its value is an “Educative”, a string.

{"company_name" : "Educative"}

Number

The number data type is used to store any numeric data in the form of an integer of float. It supports base-10 numbers only.

For example, the following code shows a JSON object with a property named age, whose value is 20.

{"age" : 20}

Boolean

The Boolean data type stores data that can be either true or false.

For example, the following code shows a JSON object with a property named account_verified whose value is false, which is a Boolean.

{"account_verified": false}

Null

The null data type is used whenever we have a property, but the value against it is undefined. The null data type is used in place of any undefined data.

For example, the following snippet shows a JSON object that holds a user’s primary and secondary email addresses.

{
"primary_email" : "abc@educative.io",
"secondary_email" : null
}

In this case, the secondary_email is null because it does not exist for this specific user.

Arrays

Arrays are a collection of similar data. An array is used to store multiple values of similar data against a single property.

An array is enclosed within square brackets and must contain the same type of data.

For example, the following code shows a JSON object which contains the names of multiple students enrolled in a class.

{"students_enrolled" : ["John", "Dave", "Peter"]}

Object

Object is a collection of name or value pairs that are in between curly brackets.

For example, the following snippet shows an object that contains a student’s name and his scores in different subjects. The scores are stored in a nested object.

{
"name" : "David",
"marks" : {"english" : 50, "mathematics" : 90, "physics" : 75}
}

RELATED TAGS

json

CONTRIBUTOR

Amaaz Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring