Learn How to Code with Python: Beginner to Advanced Guide
JSON (JavaScript Object Notation) is a lightweight data exchange format designed to be easy for humans to read and write, and easy for machines to parse and generate. Although it originated from JavaScript, it is language-independent and widely used in Python, Java, C#, Go, and many other programming environments. JSON has become the standard format for transmitting structured data over networks, especially in web APIs. At its core, JSON represents data using two primary structures: objects and arrays. Objects are collections of key-value pairs, and arrays are ordered lists of values. This simplicity is the reason JSON replaced heavier formats like XML in many applications. In Python, JSON is commonly used to send and receive data from web services, store configuration files, and serialize application data. Understanding JSON is fundamental for any Python developer working with modern software systems. 1 To create graphical user interfaces To exchange structured data between systems To replace Python as a programming language What is the main purpose of JSON? A JSON object is enclosed in curly braces `{}` and contains key-value pairs. Each key must be a string enclosed in double quotes, followed by a colon and its corresponding value. Multiple key-value pairs are separated by commas. For example: `{"name": "Alice", "age": 30}` Here, "name" and "age" are keys, and "Alice" and 30 are their associated values. Keys in JSON must always be strings. Values, however, can be strings, numbers, objects, arrays, booleans, or null. 1 With semicolons With colons With equal signs How are key-value pairs separated in a JSON object? A JSON array is enclosed in square brackets `[]` and contains an ordered list of values. Values inside arrays are separated by commas. Arrays can contain mixed data types, including numbers, strings, objects, and even other arrays. Example: `["apple", "banana", "cherry"]` This is a simple array of strings. Arrays are useful when representing collections of similar items, such as a list of users, products, or transactions. 1 Curly braces {} Square brackets [] Parentheses () Which symbol is used to enclose a JSON array? JSON supports several data types: string, number, object, array, boolean, and null. Strings must always be enclosed in double quotes. Numbers can be integers or floating-point values. Booleans are written in lowercase: `true` and `false`. The `null` value represents an empty or undefined value. It is important to note that JSON does not support comments. Including comments will cause parsing errors. 0 True FALSE true Which of the following is a valid JSON boolean? In Python, the built-in `json` module allows you to work with JSON data. The `json.loads()` function converts a JSON string into a Python dictionary, while `json.dumps()` converts a Python object into a JSON string. For example: `json.loads('{"name": "Alice"}')` converts the JSON string into a Python dictionary. `json.dumps({"name": "Alice"})` converts the dictionary back into JSON format. These functions are essential when working with APIs or reading JSON configuration files. 1 json.dumps() json.loads() json.parse() Which function converts a JSON string into a Python dictionary? Serialization is the process of converting a Python object into a format that can be stored or transmitted, such as JSON. Deserialization is the reverse process—converting JSON back into a Python object. Serialization is important when saving data to files, sending data over networks, or caching results. JSON is one of the most common serialization formats because of its simplicity and compatibility. In Python, serialization is typically done using `json.dumps()` or `json.dump()` when writing to files. 1 Converting JSON into binary code Converting a Python object into JSON format Encrypting JSON data What is serialization? JSON is often used in REST APIs. When a client sends a request to a server, the server frequently responds with JSON-formatted data. This data can then be parsed and used within the application. For example, a web API might return: `{"id": 1, "username": "john_doe", "active": true}` The client application reads this JSON response and converts it into usable Python data structures. 1 It is encrypted by default It is human-readable and easy to parse It only works with JavaScript Why is JSON widely used in web APIs? JSON syntax rules are strict. Strings must use double quotes, not single quotes. Trailing commas are not allowed after the last element in an object or array. For example, this is invalid JSON: `{"name": "Alice",}` The trailing comma causes an error. Strict syntax ensures consistent parsing across different systems and languages. 1 Using double quotes Adding a trailing comma Using numbers Which of the following makes JSON invalid? Nested JSON refers to JSON structures that contain other objects or arrays inside them. This allows representation of complex hierarchical data. Example: `{"user": {"name": "Alice", "roles": ["admin", "editor"]}}` Here, the value of "user" is another object, and "roles" is an array within that object. Nested structures are common in real-world applications such as representing user profiles or product catalogs. 1 JSON stored inside a database JSON containing objects or arrays within other objects or arrays JSON with syntax errors What is nested JSON? JSON files typically use the `.json` file extension. They are plain text files and can be opened with any text editor. Many configuration files and data storage formats rely on JSON due to its simplicity. When reading a JSON file in Python, you can use `json.load()` to parse the file directly into a Python object. When writing to a file, `json.dump()` is used. Because JSON is text-based and platform-independent, it is ideal for cross-system communication and data storage. 2 .txt .xml .json Which file extension is commonly used for JSON files?