Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Let's dive into JSON!

This explanation is for folks who might be new to the world of computers. If you're already
familiar with JSON, feel free to skip ahead. But for everyone else, let's get you comfortable
with the basics.

What is JSON?

Imagine a way to store information in a structured format that both humans and computers
can understand easily. That's JSON in a nutshell. It uses objects to organize data. Here's an
example:

JSON
{
"name": "Alice",
"age": 30,
"occupation": "Web Developer",
"skills": ["JavaScript", "HTML", "CSS"]
}
Use code with caution.
content_copy

Pretty straightforward, right? Let's break down some technical details:

• Curly braces ({ }): These mark the beginning and end of a JSON object.
• Properties (key-value pairs): Inside the object, you'll find a list of properties. Each
property has two parts:
o Key (like "name"): Identifies the specific piece of information.
o Value (like "Alice"): Holds the actual data associated with the key.
• Separators: A colon (:) separates the key and value, and commas (,) separate
different properties. Notice there's no comma after the last value.
• Quotation marks:
o Keys: Always enclosed in double quotes (").
o Values: Required for strings and phrases (like "Web Developer"). Numbers or
Boolean values (true/false) can go without quotes, but it's good practice to use
them for consistency.
o Simple vs. Fancy: Use plain double quotes (") – not the fancy ones some
programs might insert automatically. Computers see these as different
characters, and using the wrong ones can cause errors. That's why it's best to
avoid tools like Word or Google Docs for writing JSON (more on that later).
• Formatting: Indentation, spaces, and line breaks are for readability only. Computers
don't care about them.

Relax, mistakes happen!

Don't worry too much about these finer points. Most JSON editors will highlight syntax
errors and warn you. There are many online editors available, and here's a popular one:
https://jsoneditoronline.org/

Keys and Values: What's the Difference?


In our example, keys are typically consistent (we'll decide them in advance and explain them
in this documentation). However, values will usually differ for each object (you'll change
them to create different exercises). Here's another object:

JSON
{
"name": "Bob",
"age": 25,
"occupation": "Musician",
"instruments": ["Guitar", "Drums"],
"city": "New York"
}
Use code with caution.
content_copy

As you can see, most keys are present in both objects, but with different values. Bob doesn't
have an "occupation" of "Web Developer" like Alice, but he has a new property,
"instruments." Some properties might be mandatory, while others are optional. This doesn't
depend on JSON itself, but on the program that will read the JSON data. We'll make sure to
clarify which properties are essential in our specific use case.

Lists: When One Favorite Color Isn't Enough

What if someone has multiple favorite colors? Let's see how JSON handles lists:

JSON
{
"name": "Charlie",
"age": 40,
"occupation": "Artist",
"favoriteColors": ["Blue", "Green", "Purple"],
"city": "Paris"
}
Use code with caution.
content_copy

Here, the value for "favoriteColors" is no longer a single string. It's a list enclosed in square
brackets ([]) containing multiple values separated by commas. Quotation marks are still
required for strings within the list, but not for numbers. And remember, commas are needed
after each element except the last one.

A Note on Consistency

We can't suddenly change a property to hold a list if it was previously a string. So, let's
update the previous examples to use lists for consistency:

JSON
{
"name": "Alice",
"age": 30,
"occupation": "Web Developer",
"skills": ["JavaScript", "HTML", "CSS"]
}
{
"name": "Bob",
"age": 25,
"occupation": "Musician",
"instruments": ["Guitar", "Drums"],
"city": "New York"
}

You might also like