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

18 Replies, 969 Views

Dude, just found this lib called `marshmallow`.
It’s a bit overkill for simple stuff, but if you’re dealing with complex json, it’s gold.

```python
from marshmallow import Schema, fields

class UserSchema(Schema):
name = fields.Str()
age = fields.Int()

data = UserSchema().load(json.loads('{"name": "Tom", "age": 25}'))
```

Now `data["name"]` works, but you can tweak it to use dots.

Messages In This Thread



Users browsing this thread: 1 Guest(s)