JSON Syntax Overview - potatoscript/json GitHub Wiki
π JSON Syntax Overview: The Basic Rules of the JSON World!
JSON is like a language that computers use to talk to each other and exchange data. But just like any language, it has its own set of rules (called syntax) that we must follow to make sure everything works properly! π§βπ«π€
Think of JSON syntax as the grammar for writing data. And just like in a school lesson, you must follow these rules to make sure your data is correct and can be understood by everyone! π
π The Main Rules of JSON Syntax
-
Data is in Name/Value Pairs (Key/Value) π·οΈ
In JSON, we write data as name/value pairs. Think of it like a label on a shelf with an itemβs name and quantity. The name is the key and the value is what is stored.
- Key: The name (a string wrapped in quotes).
- Value: The actual data (could be a string, number, or more!).
Example:
"name": "Lucy"
In this example:
"name"
is the key."Lucy"
is the value.
-
JSON Objects (The Big Box!) π
JSON objects are like boxes that contain multiple key/value pairs. The box is wrapped in curly braces
{ }
.- Each key-value pair inside the box is separated by a comma.
- The keys and values are separated by a colon
:
.
Example:
{ "name": "Lucy", "age": 10 }
Here, the box
{ }
contains two key-value pairs:"name": "Lucy"
"age": 10
-
Arrays (The List of Items) π
JSON also has arraysβlike a list of things that go inside square brackets
[ ]
. Each item in the list is separated by a comma.- You can store many values (like names, numbers, or objects) inside an array.
Example:
{ "fruits": ["Apple", "Banana", "Grapes"] }
Here, the array
["Apple", "Banana", "Grapes"]
is the value for the key"fruits"
.
-
String Values (Words in Quotes) π
All text or words in JSON are always surrounded by double quotes
" "
.- This includes the keys and the values that are strings.
Example:
{ "greeting": "Hello, World!" }
Here,
"greeting"
is a key, and"Hello, World!"
is the value.
-
Numeric Values (No Quotes) π’
If the value is a number, you donβt need to put it in quotes. JSON understands integers and floating point numbers (like 10 or 10.5).
Example:
{ "age": 10, "height": 1.5 }
Here,
10
and1.5
are numeric values for"age"
and"height"
.
-
Boolean Values (True or False) β β
JSON supports boolean values, which can be either
true
orfalse
. These donβt need quotes, and they are often used to represent yes/no or on/off.Example:
{ "isStudent": true, "hasPet": false }
Here:
"isStudent"
is true, meaning the person is a student."hasPet"
is false, meaning the person does not have a pet.
-
Null Values (Empty Values) π«
Sometimes, a value in JSON is empty, and for that, we use null. This means the value is missing or not available.
Example:
{ "middleName": null }
Here,
"middleName"
is set to null, meaning there is no middle name for this person.
π Putting It All Together: A Full JSON Example!
Now that we know all the basic rules, letβs look at a complete JSON example! π
Hereβs a JSON object that describes a person with various attributes (name, age, height, pets, etc.):
{
"name": "Lucy",
"age": 10,
"height": 1.5,
"isStudent": true,
"middleName": null,
"fruits": ["Apple", "Banana", "Grapes"],
"address": {
"street": "123 Main St",
"city": "Wonderland"
}
}
Explanation:
- name:
"Lucy"
(string) - age:
10
(number) - height:
1.5
(number) - isStudent:
true
(boolean) - middleName:
null
(no middle name) - fruits: An array of fruits, including
"Apple"
,"Banana"
, and"Grapes"
. - address: A nested object that contains the street and city of the person.
π‘ Quick Summary of JSON Syntax:
- Key/Value Pairs: Data is written as key-value pairs, with keys in quotes and values in any data type (string, number, etc.).
- Objects: Objects are surrounded by
{ }
, with key-value pairs inside, separated by commas. - Arrays: Arrays are surrounded by
[ ]
and can contain multiple values. - Data Types:
- Strings are in double quotes (
" "
). - Numbers are written without quotes.
- Booleans are
true
orfalse
. - Null is written as
null
.
- Strings are in double quotes (