Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

JSON data Types

Posted in JSON Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 128 - Views
⏳ 4 mins
💬 1 Comment
JSON data Types

Photo Credit to CodeToFun

🙋 Introduction

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. JSON is widely used in web applications to transmit data between a server and a client.

Understanding the various data types in JSON is crucial for working with this format effectively.

🤔 What is JSON?

JSON is a text format that uses a syntax similar to JavaScript object notation, although it is language-independent. JSON data is represented as key-value pairs, where keys are strings and values can be any of the following data types.

📈 JSON Data Types

JSON supports a limited set of data types, which are essential for constructing valid JSON documents. These types include:

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

String

Strings in JSON are sequences of characters enclosed in double quotes. They can include any Unicode character and must be properly escaped if they include special characters.

JSON
Copied
Copy To Clipboard
{
  "name": "John Doe",
  "message": "Hello, World!"
}

Number

Numbers in JSON can be integers or floating-point values. They are not enclosed in quotes and can include exponents.

JSON
Copied
Copy To Clipboard
{
  "age": 30,
  "height": 5.9,
  "scientific": 1.23e4
}

Boolean

Boolean values in JSON are true or false. These values are not enclosed in quotes.

JSON
Copied
Copy To Clipboard
{
  "isStudent": true,
  "hasGraduated": false
}

Null

The null value represents an empty or non-existent value. It is not enclosed in quotes.

JSON
Copied
Copy To Clipboard
{
  "middleName": null
}

Array

Arrays in JSON are ordered lists of values. These values can be of any type, including other arrays or objects. Arrays are enclosed in square brackets and values are separated by commas.

JSON
Copied
Copy To Clipboard
{
  "fruits": ["apple", "banana", "cherry"],
  "numbers": [1, 2, 3, 4, 5],
  "mixed": ["text", 123, false, null]
}

Object

Objects in JSON are collections of key-value pairs. Keys are strings and values can be of any type. Objects are enclosed in curly braces, and each key-value pair is separated by a comma.

JSON
Copied
Copy To Clipboard
{
  "person": {
    "firstName": "Jane",
    "lastName": "Doe",
    "age": 25,
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "zipcode": "12345"
    },
    "phoneNumbers": ["555-1234", "555-5678"]
  }
}

📄 Example of a JSON Document

Here's a complete example of a JSON document that combines different data types:

JSON
Copied
Copy To Clipboard
{
  "employee": {
    "id": 101,
    "name": "Alice",
    "email": "alice@example.com",
    "isManager": false,
    "department": null,
    "skills": ["JavaScript", "Python", "HTML", "CSS"],
    "projects": [
      {
        "name": "Project A",
        "deadline": "2024-06-30",
        "budget": 10000.50
      },
      {
        "name": "Project B",
        "deadline": "2024-12-31",
        "budget": 20000.75
      }
    ]
  }
}

In this example, the JSON document describes an employee object with various attributes, including strings, numbers, booleans, null values, arrays, and nested objects.

🎉 Conclusion

Understanding JSON data types is fundamental for working with JSON. Each data type serves a specific purpose and helps in structuring the data in a meaningful way.

Whether you are developing web applications, APIs, or data storage solutions, mastering JSON data types will enhance your ability to handle data efficiently.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy