Modern QA2026Dictionaries and Objects — tiles
Log inJoin
21 / 80 · 12 Programming for QA · Data Structures for QA← prev⊞ allnext →☰ Read as one page

3.3Dictionaries and Objects

Dictionaries (Python) and objects (JavaScript) are the native format for JSON data, API responses, and configuration. You will work with them constantly.

Python Dictionaries

# API response handling
user = {"email": "test@example.com", "role": "admin", "active": True}
assert response.json()["email"] == user["email"]

# Safely access nested data
config = {
    "environments": {
        "staging": {"url": "https://staging.example.com", "timeout": 30},
        "production": {"url": "https://example.com", "timeout": 10}
    }
}
staging_url = config.get("environments", {}).get("staging", {}).get("url")
assert staging_url == "https://staging.example.com"

# Dictionary comparison for response validation
expected = {"id": 1, "name": "Alice", "role": "admin"}
actual = response.json()
# Check expected is a subset of actual (actual may have extra fields)
for key, value in expected.items():
    assert actual[key] == value, f"Mismatch on {key}: expected {value}, got {actual[key]}"

# Merge dictionaries (Python 3.9+)
default_headers = {"Content-Type": "application/json"}
auth_headers = {"Authorization": "Bearer token123"}
headers = default_headers | auth_headers

JavaScript/TypeScript Objects

// Destructuring API responses
const { id, name, email } = response.data;
expect(id).toBeDefined();
expect(name).toBe("Alice");

// Spread operator for merging
const defaultHeaders = { "Content-Type": "application/json" };
const authHeaders = { Authorization: "Bearer token123" };
const headers = { ...defaultHeaders, ...authHeaders };

// Optional chaining for safe access
const stagingUrl = config?.environments?.staging?.url;
expect(stagingUrl).toBe("https://staging.example.com");

// Check object shape
expect(Object.keys(user)).toEqual(expect.arrayContaining(["id", "name", "email"]));