Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
JSON (JavaScript Object Notation) is the standard data interchange format for modern web APIs, configuration files, cloud services, and NoSQL databases. Python provides native support for encoding and decoding JSON via its built-in json module.
JSON data maps cleanly to Python's primitive data structures. The standard conversion table is:
| Python Type | JSON Equivalent | Notes |
|---|---|---|
dict | object | JSON keys must always be strings enclosed in double quotes |
list, tuple | array | Tuples are serialized to JSON arrays |
str | string | Must use double quotes "" in JSON |
int, float | number | Same numeric representation |
True / False | true / false | Lowercase in JSON |
None | null | Lowercase null in JSON |
dump() vs. dumps()Serialization (or "encoding / marshalling") converts an in-memory Python object into a JSON-formatted representation.
json.dumps(obj): Serializes Python object into a JSON string (s stands for string).json.dump(obj, file): Serializes Python object directly into a file-like stream.json.dumps()json.dump()load() vs. loads()Deserialization (or "decoding / unmarshalling") parses JSON data into native Python data structures (dict, list, etc.).
json.loads(str): Decodes a JSON string into a Python object.json.load(file): Reads and decodes JSON directly from a file stream.json.load()json.loads()json.dump / json.dumpsindent: An integer (typically 2 or 4) specifying whitespace indentation for readable "pretty printing". If None, output is compact single-line JSON.sort_keys=True: Sorts dictionary keys alphabetically, useful for consistent caching or version control diffs.separators=(item_separator, key_separator): Defaults to (', ', ': '). Setting separators=(',', ':') eliminates whitespace for minified network payloads.ensure_ascii=False: Preserves non-ASCII characters (such as accented characters or emojis) instead of escaping them as \uXXXX.By default, Python raises a TypeError: Object of type ... is not JSON serializable when trying to serialize types like datetime, Decimal, sets, or custom class instances.
To solve this, provide a custom converter via the default parameter:
JSONDecodeErrorMalformatted JSON raises json.decoder.JSONDecodeError upon parsing. Always safeguard file and network operations with targeted error handling:
json.dump() with json.dumps()json.dump(obj, file): Serializes object directly to an open file stream.json.dumps(obj): Serializes object to a Python string (in memory).Using dumps() with a file parameter causes a TypeError.
Standard JSON does not support Python datetime, set, or custom class instances directly. Convert them to ISO strings, lists, or dictionaries first, or provide a custom default serializer.
Build a robust configuration manager that stores server settings in a config.json file. If the file exists, load the settings; if it is missing, generate default settings and write them formatted with clean indentation.
json.dump() and json.dumps() in Python?tuple to JSON, what does it become?json.dumps() to format the output with clean 4-space indentation?json.loads() on an invalid JSON string?json module raise a TypeError when attempting to serialize a set directly?In the next lesson, we will continue your intermediate Python journey with File Exceptions and Error Handling (4: File Handling Advanced).
👉 Continue to Next Lesson: File Exceptions and Error Handling →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Reading & Writing CSV Files | File Exceptions and Error Handling |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.