Contenido del Curso
Professional Web API with Flask
Professional Web API with Flask
JSON Explanation
So, now we are ready to explore the JSON topic. JSON (JavaScript Object Notation) is a text-based data format. It is language-independent, with many programming languages having built-in support for parsing and generating JSON data. JSON is widely used as a data format for RESTful APIs. It also sends data from the server to the client and vice versa.
- Lightweight: has a minimal, easy-to-read format;
- Language-Independent: can be used with most modern programming languages;
- Human-Readable: structured in a way that's intuitive and easy to comprehend;
- Easy to Parse: can be easily parsed and generated by machines.
JSON is built on two universal data structures: Objects and Arrays; Objects looks like Python dictionaries when arrays like lists.
A value in JSON can be a string, number, boolean, array, object, or null.
- String: enclosed in double quotes " ";
- Number: integer or floating-point;
- Boolean: true or false;
- Array: an ordered list of values;
- Object: an unordered collection of key/value pairs. Similar to Python dictionaries;
- Null: an empty value, denoted by null;
JSON supports nested objects and arrays, allowing for complex data structures.
But don't forget that JSON is a long string. And if we need to convert Python dict, list or object to the JSON we must stringify it. During the course we will do parsing and serialization.
Parsing (Deserialization) is the process of converting JSON text into a native data structure (as Python List, Dict, or Object). For example, user request is to get the list of all instances of languages from our DB. For that, all information from our API will be sent in and vice versa. Serialization is the process of converting a native data structure into JSON.
Flask usually uses library-based parsing capabilities such as Flask-Smorest and, specifically, Marshmallow library. We will do it in the next chapters.
¡Gracias por tus comentarios!