How do you properly python cast json as object? Best practices? or What's the correct way to python c

18 Replies, 856 Views

Honestly? Just use `attrs` instead of `dataclasses` if you want more control.
It’s like dataclasses but with extra sauce.

```python
import attr
import json

@attr.s
class Item:
id = attr.ib()
name = attr.ib()

data = Item(**json.loads('{"id": 1, "name": "thingy"}'))
```

Now `data.name` works and you get repr/eq for free.

Messages In This Thread



Users browsing this thread: 1 Guest(s)