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:
- String β For text and words π
- Number β For numbers and math π’
- Boolean β For true/false answers β β
- Null β For nothing or empty slots β«οΈ
- Object β For storing key-value pairs π©π
- 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:
25is an integer.99.99is a decimal number.170is 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:
truemeans yes or on.falsemeans 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:
nullmeans 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:
booksis 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 (cityandcountry)."graduationYear"is null, meaning no value is set.