JSON Data Types Explained with Examples

 If you are new to JSON, first read our complete beginner guide on What is JSON.

Introduction

If you already understand what JSON is, the next important step is learning about JSON data types. Data types define what kind of value a key can store inside a JSON object.

Understanding JSON data types is essential when working with APIs, web applications, and backend development.

JSON-data-types-explained-with-examples


What Are JSON Data Types?

JSON supports six main data types:

  1. String
  2. Number
  3. Object
  4. Array
  5. Boolean
  6. Null

Let’s understand each one with examples.

1️⃣ String

A string is a sequence of characters wrapped in double quotes.

Example:

{ "name": "Suraj" }

Important:

  • Strings must be inside double quotes.

  • Single quotes are not allowed in JSON.

2️⃣ Number

A number can be an integer or decimal.

Example:

{ "age": 25, "price": 99.99 }

JSON does not differentiate between integers and floats.

3️⃣ Object

An object is a collection of key-value pairs inside curly braces { }.

Example:

{ "person": { "name": "John", "age": 30 } }

Objects can contain other objects.

4️⃣ Array

An array stores multiple values inside square brackets [ ].

Example:

{ "skills": ["HTML", "CSS", "JavaScript"] }

Arrays can contain:

  • Strings
  • Numbers
  • Objects
  • Even other arrays

5️⃣ Boolean

A boolean represents true or false.

Example:

{ "isStudent": true }

Important:

  • Boolean values are not in quotes.
  • Must be lowercase: true or false

6️⃣ Null

The null type represents an empty or missing value.

Example:

{ "middleName": null }

Null is useful when a value is intentionally empty.

Complete JSON Example Using All Data Types

{ "name": "Suraj", "age": 22, "isDeveloper": true, "skills": ["HTML", "CSS", "JavaScript"], "address": { "city": "Delhi", "country": "India" }, "middleName": null }

This example uses all six JSON data types together.

Why JSON Data Types Matter

When working with APIs:

  • Data types help maintain structure.
  • They ensure correct data handling.
  • They prevent errors in applications.

Incorrect data types can break applications.

Conclusion

JSON supports six main data types: String, Number, Object, Array, Boolean, and Null. Understanding these types is essential for working with modern web applications and APIs.

Once you understand JSON data types, reading and creating JSON becomes much easier.

Comments