JSON Data Types

A data type tells us what kind of value is stored. Just like in real life we distinguish between numbers, words, and yes/no answers, JSON also supports different types of values. Understanding data types is important because each type behaves differently and is used for different purposes.

JSON supports exactly six data types. Let's explore each one clearly.

1. String

A string is a sequence of characters (letters, numbers, symbols, or spaces) enclosed in double quotes. It is used to represent text data.

{
  "city": "Bangalore",
  "greeting": "Hello, World!",
  "email": "student@example.com"
}

Important rules for strings:

  • Must always be wrapped in double quotes
  • Single quotes are not allowed
  • Special characters like " inside a string must be escaped using \"

Example with escaped character:

{
  "quote": "He said, \"JSON is easy!\""
}

2. Number

A number stores numeric values. JSON supports both whole numbers (integers) and decimal numbers (floats). Numbers are written without quotes.

{
  "age": 22,
  "price": 499.99,
  "temperature": -5,
  "distance": 1.5e3
}

Points to note:

  • No quotes around numbers
  • Negative numbers are allowed (e.g., -5)
  • Decimal numbers are allowed (e.g., 3.14)
  • Scientific notation is allowed (e.g., 1.5e3 means 1500)
  • Special values like NaN (Not a Number) and Infinity are not allowed in JSON

3. Boolean

A boolean represents one of two possible values: true or false. It is used to indicate a yes/no or on/off condition. Boolean values are written in lowercase without quotes.

{
  "isLoggedIn": true,
  "hasDiscount": false,
  "isVerified": true
}

Important: Writing "true" (with quotes) makes it a string, not a boolean. Always write true or false without quotes.

4. Null

The null value represents the intentional absence of any value. It means the field exists but currently has no value — it is empty or unknown.

{
  "middleName": null,
  "profilePicture": null,
  "lastLogin": null
}

Think of null as an empty box. The box (key) exists, but nothing is inside it. Always write null in lowercase and without quotes.

5. Object

An object is a collection of key-value pairs wrapped inside curly braces { }. Objects are used to group related data together. A value in JSON can itself be an object.

{
  "student": {
    "name": "Deepak",
    "age": 19,
    "city": "Pune"
  }
}

In this example, the value of "student" is another object containing three key-value pairs. This is called a nested object.

6. Array

An array is an ordered list of values enclosed in square brackets [ ]. Arrays are used when a key holds multiple values. The items in an array are separated by commas.

{
  "fruits": ["apple", "mango", "banana"],
  "scores": [85, 90, 78, 92],
  "flags": [true, false, true]
}

An array can hold values of the same type (all strings, all numbers) or even mixed types, including objects.

Summary of All JSON Data Types

Data TypeDescriptionExample
StringText wrapped in double quotes"name": "Anjali"
NumberInteger or decimal, no quotes"age": 25
Booleantrue or false, no quotes"active": true
NullNo value / empty"address": null
ObjectGrouped key-value pairs in { }"info": { "id": 1 }
ArrayList of values in [ ]"tags": ["html", "css"]

A Complete Example Using All Data Types

{
  "employeeName": "Sunita Rao",
  "employeeId": 1042,
  "salary": 35000.50,
  "isActive": true,
  "department": null,
  "skills": ["Python", "SQL", "Excel"],
  "address": {
    "city": "Chennai",
    "state": "Tamil Nadu"
  }
}

This single JSON object uses all six data types: string ("Sunita Rao"), number (1042), decimal number (35000.50), boolean (true), null (null), array (["Python", "SQL", "Excel"]), and object ({ "city": ... }).

Key Points to Remember

  • Strings must always be in double quotes
  • Numbers, booleans, and null are never wrapped in quotes
  • true, false, and null must be written in lowercase
  • Objects use { } and arrays use [ ]
  • Values of any type (including objects and arrays) can be nested inside each other

Leave a Comment

Your email address will not be published. Required fields are marked *