Chapter 24: Advanced Python Programming – Understanding JSON Data


24.1 Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. Though it originated from JavaScript, it is language-independent and is widely supported in modern programming languages, including Python.

JSON is commonly used for:

  • Exchanging data between web clients and servers.

  • Storing configuration files.

  • Data serialization and deserialization.

Example of a JSON Object:

{
  "name": "John Doe",
  "age": 30,
  "is_employee": true,
  "skills": ["Python", "Data Analysis", "Machine Learning"],
  "address": {
    "city": "New York",
    "zipcode": "10001"
  }
}

24.2 The json Module in Python

Python provides a built-in module called json to work with JSON data. The json module can be used to:

  • Convert Python objects to JSON (serialization or encoding)

  • Convert JSON data to Python objects (deserialization or decoding)

Importing the Module

import json

24.3 Converting Python Objects to JSON (Serialization)

Use the json.dumps() method to convert Python objects into JSON strings.

Example:

import json

data = {
    "name": "Alice",
    "age": 25,
    "is_member": True
}

json_string = json.dumps(data)
print(json_string)

Output:

{"name": "Alice", "age": 25, "is_member": true}

Parameters in dumps()

  • indent: Beautifies the output with indentation.

  • sort_keys: Sorts the keys alphabetically.

json_string = json.dumps(data, indent=4, sort_keys=True)
print(json_string)

24.4 Writing JSON to a File

Use json.dump() to write a JSON object to a file.

with open("data.json", "w") as file:
    json.dump(data, file, indent=4)

24.5 Reading JSON from a String (Deserialization)

Use json.loads() to parse JSON strings and convert them into Python objects.

json_data = '{"name": "Bob", "age": 27, "is_active": false}'
python_obj = json.loads(json_data)
print(python_obj["name"])  # Output: Bob

24.6 Reading JSON from a File

Use json.load() to read JSON data from a file and convert it to a Python object.

with open("data.json", "r") as file:
    data = json.load(file)
    print(data["name"])

24.7 JSON and Python Data Types Mapping

JSON Python
Object dict
Array list
String str
Number int/float
true True
false False
null None

24.8 Handling Complex Python Objects

The json module can only handle basic data types. For complex types like custom classes, you must define custom encoding and decoding.

Custom Encoding with default

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

def encode_person(obj):
    if isinstance(obj, Person):
        return {"name": obj.name, "age": obj.age}
    raise TypeError("Type not serializable")

person = Person("David", 40)
json_data = json.dumps(person, default=encode_person)
print(json_data)

24.9 Decoding with object_hook

def decode_person(dct):
    if "name" in dct and "age" in dct:
        return Person(dct["name"], dct["age"])
    return dct

json_str = '{"name": "David", "age": 40}'
person_obj = json.loads(json_str, object_hook=decode_person)
print(person_obj.name)

24.10 Working with Nested JSON

JSON often includes nested objects. Python can handle these using dictionaries within dictionaries.

nested_json = {
    "company": "ABC Corp",
    "employees": [
        {"name": "Alice", "dept": "HR"},
        {"name": "Bob", "dept": "IT"}
    ]
}

print(nested_json["employees"][1]["name"])  # Output: Bob

24.11 Exception Handling with JSON

Using try-except blocks helps prevent crashes due to malformed JSON.

try:
    data = json.loads('{"name": "Tom", "age": 30}')
except json.JSONDecodeError as e:
    print("Invalid JSON:", e)

24.12 Practical Use Cases

  • Web APIs: Most APIs return JSON data. Python can process API responses using requests.get().json().

  • Configurations: Application settings and preferences are often stored in .json files.

  • Data Storage: JSON is lightweight, making it suitable for saving structured data.


24.13 Advantages and Limitations of JSON in Python

Advantages:

  • Readable and lightweight.

  • Native support in Python.

  • Widely adopted for APIs.

Limitations:

  • Cannot store functions or classes directly.

  • Not ideal for binary data.

  • Large files may be inefficient to parse.


24.14 Conclusion

Understanding JSON in Python is essential for advanced programming, especially in web development, data processing, and configuration management. Python’s json module provides a simple yet powerful interface to work with JSON data effectively. By mastering serialization, deserialization, and customization, programmers can build robust and data-driven applications.


24.15 Exercises

  1. Write a Python program that converts a Python dictionary to a JSON string and saves it to a file.

  2. Create a JSON string that includes a nested object and parse it using json.loads().

  3. Write a custom encoder for a Student class with fields: name, roll_no, and marks.

  4. Using requests, fetch JSON data from a public API and extract specific fields.

  5. Identify and correct errors in the following JSON string:

{
  name: "Tom", 
  age: "30", 
  isStudent: False
}


Comments