Skip to content

(E). What is JSON

Zamathuli Luthuli edited this page Oct 30, 2023 · 3 revisions

Introduction

Welcome back!

Before we develop our basic REST API, let us expand our understanding of JSON.

JSON is an acronym for 'JavaScript Object Notation.' It's a simple, compact, efficient, and web-friendly data interchange format typically used to transfer data between a client and a server. Web developers and API creators often choose it because of its compatibility, readability, and ease of use. Therefore, when working with REST APIs, you will receive JSON data in the HTTP request and respond with JSON data in the HTTP response.

JSON Data Structure

JSON (JavaScript Object Notation) supports two primary data structures at the top level: objects or arrays (lists).

  • An object (akin to dictionaries in Python): An object in JSON is a collection of key-value pairs enclosed in curly brackets '{}.' Each key is a string; the corresponding value can be a string, number, boolean (true or false), object, array, or null. Objects utilize named properties to represent structured data.
{
    "key": "value",
    "students": 25,
    "listic_data": [
        1,
        3,
        7
    ],
    "sub_objects": {
        "name": "Zoe",
        "age": 25
    }
}
  • Then, with a comma, you separate one key and value pair from another key and value pair.
  • An array: An array in JSON is an ordered list of values enclosed in square brackets '[].' Each value can be a string, number, boolean (true or false), object, array, or null. In simple terms, arrays represent ordered collections of values.
  • A JSON cannot have a string or a number since that would not be a valid JSON; therefore, everything has to be inside lists or objects.

Here's a simple example illustrating both an object and an array at the top level of JSON:

[
    {
        "name": "Zoe",
        "age": 25
    },
    {
        "name": "Jonathan",
        "age": 27
    },
    {
        "name": "John",
        "age": 23
    }
]

In Conclusion

JSON is a text string and not a Python dictionary, so when you have a Python dictionary and want to turn it into JSON, you must stringify it. You must turn it into a string to send it over the Internet. However, an essential thing regarding our REST API, Flask provides us the benefit of automatically converting a Python dictionary into JSON when we return it in a Flask route, saving us time and effort.

Note that "turning it into JSON" means two things:

  1. Change Python keywords and values to match the JSON standard (e.g., True to true). In Python, 'True' goes with a capital T, but when you jsonify it, which is the technical term, you will use a lowercase 't,' so that it is a valid JSON.
  2. Turn the whole JSON data into a single string that our API can return.

Therefore, when we return JSON with our REST API and our browser receives it, it does not look pretty because it receives the text. It still needs to receive a Python dictionary. Hence, JSON can be "prettified," as shown in our previous example, and "non-petrified." However, our API usually returns JSON in a "not-prettified" format, as illustrated in the following code.

[{"name":"Zoe","age":25},{"name":"Jonathan","age":27},{"name":"John","age":23}]

Therefore, it takes up fewer bytes because there are no spaces in between, so it is smaller. And in the end, you can save quite a lot of bandwidth space because there is lesser data to transfer between the API and the client if you return a non-prettified JSON.

That's the essence of what JSON is. A long string with a specific format can include strings, numbers, boolean, lists, and other sub-objects; that is how clients and APIs often communicate.

Here's the link to the following section of the tutorial: Getting Started with Insomnia

Clone this wiki locally