gitmyhub

marshmallow

Python ★ 7.2k updated 1d ago

A lightweight library for converting complex objects to and from simple Python datatypes.

A Python library for converting complex objects to and from plain data formats like JSON, and for validating incoming data against rules you define, all configured once in a single schema class that your whole app reuses.

Pythonsetup: easycomplexity 2/5

Marshmallow is a Python library that helps you move data cleanly between your application and the outside world. When your app works with objects that have nested structures, dates, or custom types, marshmallow handles the translation into plain Python dictionaries and strings that can be sent as JSON or stored in a database. It also works in reverse, taking raw incoming data and converting it back into the objects your code expects.

The core concept is a "schema," which is a class you write that describes the shape of your data. You define which fields exist, what types they should be, and any rules they must follow. Once you have a schema, you can use it to serialize (convert your objects to simple data), deserialize (convert incoming data to objects), and validate (check that the incoming data meets your rules). All three operations come from the same schema definition, so you only describe your data once.

Marshmallow works with any Python web framework or database library. It does not care whether you are using Flask, Django, SQLAlchemy, or something else entirely. You define schemas as plain Python classes, and the library stays out of the way of your broader codebase. This makes it a common choice in HTTP API projects where data needs to be validated on the way in and formatted on the way out.

Installing marshmallow takes one command via pip, and the full documentation is hosted at marshmallow.readthedocs.io. The project has an active ecosystem of third-party extensions listed in its GitHub wiki, covering integrations with popular frameworks and tools. The library is MIT licensed and maintained through open-source contributions, with professional support available via a Tidelift subscription for teams that need it.

Where it fits