No history yet

JSON Basics

What Is JSON?

JSON, which stands for JavaScript Object Notation, is a simple, text-based format for representing and storing data. Think of it as a universal language that different computer programs can use to talk to each other. Because it's easy for both humans to read and machines to parse, it has become the most common way to send data between web servers and applications.

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that's easy for humans to read and write, and easy for machines to parse and generate.

The structure of JSON is built on two simple concepts: a collection of name/value pairs, and an ordered list of values.

Syntax and Structure

JSON data is organized in key-value pairs. A key is always a string in double quotes, followed by a colon, and then its value. This structure makes the data self-describing. For example, you don't just see the value "Alice"; you see "name": "Alice", which tells you exactly what "Alice" represents.

The basic format is "key": value.

JSON has a few simple rules:

  • Data is in key/value pairs.
  • Keys must be strings, written with double quotes.
  • Data is separated by commas.
  • Curly braces {} hold objects.
  • Square brackets [] hold arrays.

The Six Data Types

Every value in JSON must be one of six data types. This keeps the format consistent and predictable.

Data TypeDescriptionExample
StringA sequence of text characters, wrapped in double quotes."hello world"
NumberAn integer or a floating-point number.42 or 3.14
BooleanA simple true or false value.true
ArrayAn ordered list of values, enclosed in square brackets [].[ "apple", "banana", "cherry" ]
ObjectAn unordered collection of key-value pairs, in curly braces {}.{ "name": "John", "age": 30 }
NullRepresents an empty or non-existent value.null

Objects and arrays are the containers that give JSON its structure. An object is like a dictionary, mapping keys to values. An array is a simple list.

Here’s a more complete example showing how these types can be combined to describe a user profile.

{
  "firstName": "Jane",
  "lastName": "Doe",
  "isStudent": true,
  "age": 28,
  "courses": [
    {
      "title": "History 101",
      "credits": 3
    },
    {
      "title": "Physics 205",
      "credits": 4
    }
  ],
  "address": null
}

In this example, the entire structure is a single JSON object. The values include strings ("Jane"), a boolean (true), a number (28), an array of objects (for courses), and a null value (null).

Lesson image

JSON vs. XML

Before JSON became popular, XML (eXtensible Markup Language) was a common choice for data exchange. While both can represent the same data, they have key differences. JSON is generally less verbose and easier to read. It also maps more directly to the data structures used in modern programming languages.

// JSON Example
{
  "user": {
    "name": "John Smith",
    "email": "john@example.com"
  }
}

Now, here is the same data represented in XML:

<!-- XML Example -->
<user>
  <name>John Smith</name>
  <email>john@example.com</email>
</user>

Notice how XML uses opening and closing tags for every element, making it longer. For complex data, this difference becomes even more significant, making JSON a more lightweight and efficient choice for most web applications.

Let's review what we've learned.

Quiz Questions 1/5

What is the primary purpose of JSON?

Quiz Questions 2/5

In a JSON object, keys must always be strings enclosed in double quotes.

Understanding these fundamentals is the first step to working with data on the web.