{"library":"marshmallow-jsonapi","title":"marshmallow-jsonapi","description":"marshmallow-jsonapi provides JSON:API 1.0 (https://jsonapi.org) formatting capabilities on top of the popular marshmallow serialization/deserialization library. It helps developers create schemas that comply with the JSON:API specification for building REST APIs. The current version is 0.24.0, and it generally follows the release cadence of its core dependency, marshmallow, with releases typically tied to marshmallow updates or bug fixes.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install marshmallow-jsonapi"],"cli":null},"imports":["from marshmallow_jsonapi import Schema","from marshmallow_jsonapi.fields import Relationship","from marshmallow import fields"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import datetime as dt\nfrom marshmallow import fields\nfrom marshmallow_jsonapi import Schema\n\n\nclass AuthorSchema(Schema):\n    id = fields.Str(dump_only=True)\n    first_name = fields.Str(required=True)\n    last_name = fields.Str(required=True)\n    date_created = fields.DateTime(dump_only=True)\n\n    class Meta:\n        type_ = 'authors'\n        strict = True\n\n\nclass BookSchema(Schema):\n    id = fields.Str(dump_only=True)\n    title = fields.Str(required=True)\n    pages = fields.Int()\n    author = fields.Relationship(\n        related_url='/authors/{author_id}',\n        related_url_kwargs={'author_id': '<author.id>'}, \n        attribute='author', \n        type_='authors'\n    )\n\n    class Meta:\n        type_ = 'books'\n        strict = True\n\n\n# Example Usage\nauthor_data = {\n    'id': '1',\n    'first_name': 'John',\n    'last_name': 'Doe',\n    'date_created': dt.datetime.now()\n}\nauthor_schema = AuthorSchema()\nserialized_author = author_schema.dump(author_data)\nprint('Serialized Author:')\nprint(serialized_author)\n\nbook_data = {\n    'id': '101',\n    'title': 'The Great Book',\n    'pages': 300,\n    'author': author_data # Pass the entire author object for relationship handling\n}\nbook_schema = BookSchema()\nserialized_book = book_schema.dump(book_data)\nprint('\\nSerialized Book:')\nprint(serialized_book)\n","lang":"python","description":"This quickstart defines two JSON:API compliant schemas for 'authors' and 'books'. It demonstrates the use of `marshmallow_jsonapi.Schema` for the base schema, standard `marshmallow.fields`, and `marshmallow_jsonapi.fields.Relationship` for defining JSON:API relationships. The crucial `Meta.type_` attribute is set, which is required for JSON:API resource objects. It then serializes example data for both.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}