</> Code Editor { } Code Formatter

Python Working with JSON Exercises


1/20

Python Working with JSON Practice Questions

Correct
0%

In the Python json library, what is the primary difference between the json.dump() function and the


The "s" in dumps and loads stands for String.

Memory vs. Storage:

  • json.dumps(obj): Useful when you want to send JSON over a network or store it in a database as a string.
  • json.dump(obj, file_ptr): Useful when you want to save your data directly into a .json file on your hard drive.
Mnemonic: Just remember: dump (to a file) vs. dump-S (to a String).

Quick Recap of Python Working with JSON Concepts

If you are not clear on the concepts of Working with JSON, you can quickly review them here before practicing the exercises. This recap highlights the essential points and logic to help you solve problems confidently.

Working with JSON — Definition, Mechanics, and Usage

JSON (JavaScript Object Notation) is a lightweight, text-based data format used universally for data exchange. In Python, the json module translates between Python objects (dictionaries, lists) and JSON strings or files. This is essential for web APIs, configuration files, and persistent data storage.

Because JSON structure closely mirrors Python's native dictionary and list types, it is the most common format for passing data between a Python backend and a web frontend or external service.

Why Use JSON — Key Benefits

JSON is preferred over other formats like XML or Pickle because it is easier to read, faster to parse, and supported by every modern programming language.

BenefitDetailed Explanation
Language AgnosticJSON is supported by virtually every language, enabling Python to communicate with JavaScript, Java, or C++.
Strict MappingPython's native types (dict, list, str, int) map directly to JSON types (object, array, string, number).
PersistenceProvides a simple way to save complex data structures to a .json file and reload them later without losing structure.
Human-ReadableUnlike binary formats, JSON is plain text, making it easy for developers to debug and edit configuration files.

Serialization: Python to JSON (dump and dumps)

Serialization is the process of converting a Python object into a JSON-formatted string. This is necessary when you want to send data over a network or save it to a disk.

  • json.dumps(): The "s" stands for string. It returns a JSON-formatted string.
  • json.dump(): Used for files. It writes JSON data directly to a file stream.
import json

data = {
    "user": "Alice",
    "score": 95,
    "verified": True,
    "tags": ["python", "dev"]
}

# 1. Serialization to String
json_string = json.dumps(data)
print(f"Serialized String: {json_string}")

# 2. Serialization to File (Handling .json Files)
with open("profile.json", "w", encoding='utf-8') as f:
    json.dump(data, f)

Deserialization: JSON to Python (load and loads)

Deserialization is the inverse process: transforming a JSON string or file back into a Pythonic data structure (usually a dictionary or list) so your code can manipulate the data.

  • json.loads(): Parses a JSON-formatted string.
  • json.load(): Parses a JSON file from a file stream.
import json

# 1. Deserialization from String
json_input = '{"status": "active", "code": 200, "data": null}'
python_dict = json.loads(json_input)

# JSON 'null' automatically becomes Python 'None'
print(f"Status: {python_dict['status']}, Data: {python_dict['data']}")

# 2. Deserialization from File
with open("profile.json", "r", encoding='utf-8') as f:
    loaded_data = json.load(f)
    print(f"Loaded User from File: {loaded_data['user']}")

Pretty Printing and Sorting

Raw JSON is often a single line of text, which is efficient for machines but difficult for humans to read. Python provides parameters to format the output for better readability in logs or configuration files.

import json

raw_data = {"c": 3, "a": 1, "b": 2, "nested": {"id": 10, "status": "ok"}}

# indent: Adds spaces/formatting for a visual hierarchy
# sort_keys: Alphabetizes keys to ensure consistent output
pretty_json = json.dumps(raw_data, indent=4, sort_keys=True)

print(pretty_json)
# Resulting output is structured and easy to audit

Handling JSON Errors (Robust Code)

When working with external APIs, the data you receive might be malformed or truncated. If you do not handle JSONDecodeError, your entire application will crash when it encounters an invalid string.

from json import JSONDecodeError
import json

invalid_json = '{"name": "Bob", "age": 30' # Missing closing brace

try:
    data = json.loads(invalid_json)
except JSONDecodeError as e:
    print(f"Critical Error: Failed to parse JSON.")
    print(f"Error Details: {e.msg} at line {e.lineno}, column {e.colno}")

Custom Encoders: Handling Sets and Classes

By default, the json module cannot serialize types like set, complex, or Custom Class Instances. You must provide a default function to convert these into JSON-supported types (like lists or dictionaries).

class Task:
    def __init__(self, title, status):
        self.title = title
        self.status = status

def my_encoder(obj):
    # Check if the object is our custom class
    if isinstance(obj, Task):
        return obj.__dict__  # Return the internal dictionary of the class
    # Handle sets (JSON doesn't have a 'set' type, only 'array')
    if isinstance(obj, set):
        return list(obj)
    raise TypeError(f"Object of type {type(obj)} is not JSON serializable")

# Usage with the 'default' parameter
task_json = json.dumps(Task("Write Code", "Done"), default=my_encoder)
print(task_json)

Best Practices With JSON

  • Always Use UTF-8: When opening files for JSON, always specify encoding='utf-8' to ensure compatibility across different operating systems.
  • Validate External Data: Never assume an API response is valid JSON. Always wrap loads() in a try...except block.
  • Security First: Avoid serializing dictionaries that contain sensitive data like passwords, private keys, or internal system paths.
  • Compact for Network: Only use indent for local files or debugging. For network transmission, omit indentation to save bandwidth and improve performance.

Summary: Key Points

  • Serialization: Use dumps (string) and dump (file) to move from Python to JSON.
  • Deserialization: Use loads (string) and load (file) to move from JSON to Python.
  • Data Mapping: JSON null → Python None; true/falseTrue/False.
  • Readability: Use indent and sort_keys for human-friendly debugging.
  • Extensibility: Use the default argument in dumps to handle non-serializable types like custom classes.


About This Exercise: Working with JSON in Python

In the modern era of web development and data science, JSON (JavaScript Object Notation) is the undisputed language of data exchange. At Solviyo, we know that being a great Python developer means more than just knowing basic syntax; it’s about knowing how to talk to other systems. Whether you are consuming a REST API or saving user settings to a configuration file, you need to be comfortable moving between Python dictionaries and JSON strings. We’ve designed these Python exercises to help you master the json module, covering everything from basic parsing to advanced custom serialization.

We’re moving beyond simple data types. These exercises will push you to handle complex nested structures and common pitfalls like date formatting and non-serializable objects. You’ll tackle MCQs and coding practice that explain the difference between json.load() and json.loads(), ensuring you never get confused by file streams and strings again. By the end of this section, you'll be able to manipulate data pipelines with confidence and precision.

What You Will Learn

This section is designed to turn data manipulation into a seamless habit. Through our structured Python exercises with answers, we’ll explore:

  • Serialization: Using json.dump() and json.dumps() to convert Python objects into JSON-ready strings.
  • Deserialization: Mastering json.load() and json.loads() to transform JSON back into Pythonic data structures.
  • Handling Files: Working directly with .json files to read and write persistent data.
  • Pretty Printing: Using indentation and sorting keys to create human-readable JSON outputs.
  • Custom Encoders: Learning how to handle types that JSON doesn't support by default, like sets or custom class instances.

Why This Topic Matters

Why do we care about JSON? Because it is everywhere. Almost every web API in existence uses JSON to communicate. If your code can't speak JSON, it’s isolated. Mastering this topic allows your Python scripts to interact with frontend frameworks, mobile apps, and cloud services. It’s the primary way we bridge the gap between different programming languages and platforms.

From a professional perspective, efficient JSON handling is a major part of building scalable backends. Understanding how to serialize data correctly prevents bugs during data transmission and ensures your application can handle the high-speed demands of modern traffic. Whether you're a data engineer or a full-stack dev, these skills are non-negotiable for building interconnected software systems.

Start Practicing

Ready to master the web’s favorite data format? Every one of our Python exercises comes with detailed explanations and answers to help you bridge the gap between theory and code. We break down the mapping between Python and JSON types so you can write cleaner, more efficient parsers. If you need a quick refresh on the difference between a string and a stream, check out our "Quick Recap" section before you dive in. Let’s see how you handle real-world data structures.