Skip to content

Instantly share code, notes, and snippets.

@Pebaz
Pebaz / json_to_pyobj.py
Created April 12, 2019 15:08
Turn JSON into Python Object
"""
Building a Python object from JSON isn't as straightforward as it looks.
SimpleNamespace only allows you to access fields that are valid variable
names. This poses a problem for JSON that contains invalid characters.
Here is my solution:
```
# Doesn't work:
>>> data = json.loads(some_str, object_hook=lambda d: SimpleNamespace(**d))