No history yet

JSON Basics

What is JSON?

JSON stands for JavaScript Object Notation. It's a simple, text-based format for storing and exchanging data. Think of it as a universal language that different applications can use to talk to each other, regardless of what programming language they're written in.

Its design is lightweight and easy for humans to read and write. More importantly, it's easy for computers to parse and generate, which has made it the most common data format for APIs on the web.

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.

Syntax and Structure

JSON's structure is built on two simple concepts: key-value pairs and ordered lists.

Objects are collections of key-value pairs. An object begins with { and ends with }. Each key is a string in double quotes, followed by a colon :, and then its value. Key-value pairs are separated by commas.

{
  "firstName": "Jane",
  "lastName": "Doe",
  "age": 32
}

Arrays are ordered lists of values. An array begins with [ and ends with ]. The values inside are separated by commas.

[
  "apple",
  "banana",
  "orange"
]

These structures can be nested to represent more complex data. For example, an object can have a value that is an array, and an array can contain objects.

{
  "squadName": "Super Hero Squad",
  "members": [
    {
      "name": "Molecule Man",
      "age": 29,
      "secretIdentity": "Dan Jukes"
    },
    {
      "name": "Madame Uppercut",
      "age": 39,
      "secretIdentity": "Jane Wilson"
    }
  ]
}

A key rule in JSON is that keys must always be strings enclosed in double quotes. Single quotes are not allowed.

Data Types

JSON values can be one of six specific data types. This limited set of types is one reason JSON is so simple and widely compatible.

Data TypeDescriptionExample
StringA sequence of characters in quotes"Hello, world!"
NumberAn integer or a floating-point number101 or 3.14
BooleanA value that is either true or falsetrue
ArrayAn ordered list of values[1, "two", false]
ObjectA collection of key-value pairs{"key": "value"}
NullRepresents an empty or null valuenull

Here's an example that combines all of these data types into a single JSON object.

{
  "id": 12345,
  "productName": "Super Widget",
  "inStock": true,
  "price": 19.99,
  "features": [
    "Durable",
    "Lightweight",
    "Waterproof"
  ],
  "warrantyInfo": null
}

Common Uses

Because of its simplicity and readability, JSON has become the standard for many applications.

APIs: When you use a mobile app to check the weather or a website to get stock prices, it's likely communicating with a server in the background. That server often sends the requested information back to the app or website formatted as JSON.

Configuration Files: Many software tools, from web servers to code editors, use .json files to store settings. This makes the configuration easy for both humans and the program to read and modify.

Data Storage: While not a full database, JSON is great for storing structured data in a simple text file. This is common for transmitting data between different parts of a large system or for simple data logging.

Let's review the key concepts we've covered about JSON.

Now, test your understanding of JSON's structure and data types.

Quiz Questions 1/5

What does JSON stand for?

Quiz Questions 2/5

In JSON, how is a collection of key-value pairs represented?

Understanding these fundamentals is the first step. Next, we'll see how to actually work with this data in a program.