Best way to convert json to dict in function python? Need help! or How to properly handle json to dic

22 Replies, 1783 Views

For json to dict in function python, don’t forget to check if the input is *actually* a string.

Sometimes people pass bytes or other types.

```python
def json_to_dict(json_str):
if isinstance(json_str, bytes):
json_str = json_str.decode('utf-8')
return json.loads(json_str)
```

Saved me a few headaches!

Messages In This Thread



Users browsing this thread: 1 Guest(s)