Hey! For json to dict in function python, you could use a decorator to handle errors gracefully.
```python
def handle_json_errors(func):
def wrapper(json_str):
try:
return func(json_str)
except json.JSONDecodeError:
return {"error": "Invalid JSON"}
return wrapper
@handle_json_errors
def json_to_dict(json_str):
return json.loads(json_str)
```
Makes the function cleaner, I think.
```python
def handle_json_errors(func):
def wrapper(json_str):
try:
return func(json_str)
except json.JSONDecodeError:
return {"error": "Invalid JSON"}
return wrapper
@handle_json_errors
def json_to_dict(json_str):
return json.loads(json_str)
```
Makes the function cleaner, I think.
