JSON Data Types - potatoscript/json GitHub Wiki

🎁 JSON Data Types: The Magic Toolbox!

Imagine you have a magic toolbox that can store different kinds of items. Each item in the toolbox serves a special purpose. In the world of JSON, these items are called data types. 🧰✨

When you create a JSON object or array, you need to decide what kind of values to store. JSON gives you six types of values to choose from, each with its own unique characteristics.


πŸ”‘ The 6 Main JSON Data Types:

  1. String – For text and words πŸ“
  2. Number – For numbers and math πŸ”’
  3. Boolean – For true/false answers βœ…βŒ
  4. Null – For nothing or empty slots ⚫️
  5. Object – For storing key-value pairs πŸŽ©πŸ“š
  6. Array – For storing lists πŸ“‹

Let’s explore them one by one! 🌟


πŸ“ 1. Strings: Words and Text

A string is a piece of text, wrapped inside double quotes " or single quotes '. Strings can hold letters, numbers, symbols, and even spaces.

πŸ“š Example: Basic Strings

{
  "name": "Lucy",
  "city": "Tokyo",
  "language": "Japanese"
}

Explanation:

  • "name", "city", and "language" are the keys.
  • "Lucy", "Tokyo", and "Japanese" are string values.

πŸ”‘ Fun Fact:

Strings in JSON can hold almost any text, but they must be enclosed in double quotes. Single quotes (') are not allowed in JSON.

🚨 Invalid String Example

{
  'name': 'Lucy'  // ❌ Incorrect
}

βœ… Use double quotes instead:

{
  "name": "Lucy"
}

πŸ”’ 2. Numbers: All About Digits

A number in JSON can be an integer (whole number) or a decimal (floating-point number). It’s used for anything involving math or counting. You don’t need quotes for numbers! 🎯

πŸ“Š Example: Basic Numbers

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

Explanation:

  • 25 is an integer.
  • 99.99 is a decimal number.
  • 170 is another integer.

⚠️ Be Careful:

Numbers can’t be written as strings. This is invalid:

{
  "age": "25"  // ❌ Incorrect, this is a string!
}

βœ… Correct:

{
  "age": 25  // βœ… Correct, this is a number!
}

βœ… 3. Boolean: True or False

A boolean is like a switch. It can only be true or false. It’s useful when you want to store data that answers a yes/no or on/off type of question. πŸ”₯❄️

πŸ”Ž Example: Boolean Values

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

Explanation:

  • true means yes or on.
  • false means no or off.

πŸ€” Fun Fact:

Booleans are always written in lowercase (true and false). Writing them as "True" or "False" (with quotes) makes them strings, not booleans.


⚫️ 4. Null: The Empty Spot

Null is like an empty box. It’s used when you want to say, β€œThere’s nothing here.” It’s often used when the value is unknown, missing, or not available. πŸ•³οΈβŒ

πŸ•°οΈ Example: Using Null

{
  "middleName": null,
  "phoneNumber": null
}

Explanation:

  • null means no value.
  • It’s different from a blank string (""), which is an empty text.

⚠️ Warning:

Don’t use quotes around null, or it becomes a string.

{
  "middleName": "null"  // ❌ Incorrect!
}

βœ… Correct:

{
  "middleName": null
}

🎩 5. Objects: Key-Value Pairs

A JSON object is a collection of key-value pairs. It’s like a dictionary where each key has a specific value. Objects are enclosed in curly braces { }. πŸ•°οΈπŸ“š

πŸ“– Example: JSON Object

{
  "title": "Harry Potter",
  "author": "J.K. Rowling",
  "year": 1997
}

Explanation:

  • "title", "author", and "year" are the keys.
  • The values can be a string, number, or even another object.

πŸ“‹ 6. Arrays: Lists of Values

An array is a list of values enclosed in square brackets [ ]. Arrays are used to store multiple values in a single variable. You can store strings, numbers, booleans, null, objects, and even other arrays in a JSON array. πŸŽ―πŸ“‹

πŸ“š Example: JSON Array

{
  "books": [
    "Harry Potter",
    "The Hobbit",
    "Percy Jackson"
  ]
}

Explanation:

  • books is an array containing three strings.
  • Arrays can store any type of value and keep them ordered.

⚑ JSON Data Types in Action!

Let’s combine everything into one big example to see how JSON data types can work together. πŸŽ‰


🌟 Final Example: JSON with All Data Types

{
  "name": "Lucy",
  "age": 25,
  "isStudent": false,
  "hobbies": ["coding", "reading", "running"],
  "address": {
    "city": "Tokyo",
    "country": "Japan"
  },
  "graduationYear": null
}

Explanation:

  • "name" is a string.
  • "age" is a number.
  • "isStudent" is a boolean.
  • "hobbies" is an array of strings.
  • "address" is an object with two keys (city and country).
  • "graduationYear" is null, meaning no value is set.