# Python JSON Handling

### 1. Concept Overview

**JSON (JavaScript Object Notation)** is a lightweight, text-based data interchange format widely used for:

* API communication
* Configuration management
* Data storage
* Inter-service messaging

Python provides native support through the built-in `json` module, enabling seamless serialization and deserialization between Python objects and JSON strings.

> JSON is the backbone format for modern microservices, REST APIs, and distributed systems.

***

### 2. JSON vs Python Data Structures

| JSON Type | Python Equivalent |
| --------- | ----------------- |
| Object    | dict              |
| Array     | list              |
| String    | str               |
| Number    | int / float       |
| Boolean   | bool              |
| null      | None              |

This mapping enables transparent conversion.

***

### 3. JSON Serialization (Python → JSON)

#### `json.dumps()` – Convert to JSON string

```python
import json

data = {"name": "Alice", "age": 30}
json_string = json.dumps(data)

print(json_string)
```

Output:

```json
{"name": "Alice", "age": 30}
```

***

### 4. JSON Deserialization (JSON → Python)

#### `json.loads()` – Convert JSON string to Python object

```python
json_data = '{"name": "Bob", "age": 25}'
python_obj = json.loads(json_data)

print(python_obj)
```

Returns a Python dictionary.

***

### 5. Reading JSON from File

```python
import json

with open("data.json", "r") as file:
    data = json.load(file)

print(data)
```

Used for:

* Config files
* System metadata
* Application state persistence

***

### 6. Writing JSON to File

```python
import json

data = {"product": "Laptop", "price": 1500}

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

Creates structured and readable output.

***

### 7. Pretty Printing with Indentation

```python
json.dumps(data, indent=4, sort_keys=True)
```

Enhances:

* Debugging
* Config readability
* Log traceability

***

### 8. Handling Complex Objects

By default, JSON supports only primitive types. Custom objects require serialization logic.

```python
class User:
    def __init__(self, name, age):
        self.name = name
        self.age = age

def serialize_user(obj):
    return obj.__dict__

print(json.dumps(User("Alice", 25), default=serialize_user))
```

***

### 9. Custom Deserialization

```python
def deserialize_user(data):
    return User(data["name"], data["age"])

json_str = '{"name":"John","age":40}'
user = deserialize_user(json.loads(json_str))
```

Used for restoring object state.

***

### 10. Enterprise Example: API Payload Processing

```python
def process_api_response(response):
    parsed = json.loads(response)
    return parsed.get("status")
```

Common in:

* REST clients
* Webhook handlers
* Microservice orchestration

***

### 11. JSON Schema Validation

Using libraries like `jsonschema` (enterprise pattern):

```python
from jsonschema import validate

schema = {"type": "object", "properties": {"name": {"type": "string"}}}
validate(instance={"name": "Alice"}, schema=schema)
```

Ensures structured compliance in production APIs.

***

### 12. Error Handling in JSON Parsing

```python
try:
    data = json.loads('{"name": "Alice"')
except json.JSONDecodeError:
    print("Invalid JSON format")
```

Prevents runtime crashes in production services.

***

### 13. Streaming Large JSON Files

```python
import json

with open("big.json") as f:
    for line in f:
        record = json.loads(line)
        process(record)
```

Supports scalable log ingestion pipelines.

***

### 14. Performance Optimization Strategies

| Technique          | Impact             |
| ------------------ | ------------------ |
| Use ujson/orjson   | Faster parsing     |
| Avoid deep nesting | Readability        |
| Stream processing  | Memory efficiency  |
| Partial loading    | Speed optimization |

***

### 15. JSON vs Other Formats

| Format | Feature         |
| ------ | --------------- |
| JSON   | Human-readable  |
| XML    | Verbose         |
| YAML   | Config-friendly |
| Pickle | Python-only     |

JSON is the most interoperable format.

***

### 16. Security Considerations

JSON handling best practices:

* Validate structure
* Avoid executing dynamic content
* Sanitize user inputs
* Use schemas for payload validation

Prevents injection risks and malformed payload attacks.

***

### 17. Enterprise Use Case: Configuration Management

```python
config = {
    "host": "localhost",
    "port": 8080,
    "debug": True
}

with open("config.json", "w") as f:
    json.dump(config, f, indent=2)
```

Makes applications easily configurable.

***

### 18. JSON in Cloud Architecture

JSON acts as the lingua franca for:

* REST APIs
* GraphQL payloads
* Kubernetes configs
* Serverless event inputs
* Message brokers (Kafka, RabbitMQ)

***

### 19. Best Practices

* Always validate JSON input
* Use `indent` for human-readability
* Apply schema validation
* Stream for large datasets
* Avoid over-nesting

***

### 20. Common Mistakes

* Not handling decode errors
* Mixing single and double quotes inconsistently
* Assuming all JSON is trusted
* Using JSON for Python-only internal objects
* Storing sensitive data unencrypted

***

### 21. Architectural Value

JSON Handling enables:

* Standardized data exchange
* Interoperable APIs
* System integration
* Cross-platform communication
* Cloud-native workflows

It forms the backbone of:

* SaaS applications
* AI inference services
* IoT data pipelines
* Microservice ecosystems

***

### Summary

Python JSON handling provides:

* Seamless object serialization
* Safe data transformation
* Efficient API communication
* Structured data storage
* Enterprise-ready payload handling

It is one of the most critical tools for modern Python system architecture.

***
