Skip to main content

JSON API 1.0 (https://jsonapi.org) formatting with marshmallow

Project description

Latest version Travis-CI

Homepage: http://marshmallow-jsonapi.rtfd.org/

JSON API 1.0 (https://jsonapi.org) formatting with marshmallow.

marshmallow-jsonapi provides a simple way to produce JSON API-compliant data in any Python web framework.

from marshmallow_jsonapi import Schema, fields

class PostSchema(Schema):
    id = fields.Str(dump_only=True)
    title = fields.Str()

    author = fields.Relationship(
        '/authors/{author_id}',
        related_url_kwargs={'author_id': '<author.id>'},
    )

    comments = fields.Relationship(
        '/posts/{post_id}/comments',
        related_url_kwargs={'post_id': '<id>'},
        # Include resource linkage
        many=True, include_data=True,
        type_='comments'
    )

    class Meta:
        type_ = 'posts'
        strict = True

post_schema = PostSchema()
post_schema.dump(post).data
# {
#     "data": {
#         "id": "1",
#         "type": "posts"
#         "attributes": {
#             "title": "JSON API paints my bikeshed!"
#         },
#         "relationships": {
#             "author": {
#                 "links": {
#                     "related": "/authors/9"
#                 }
#             },
#             "comments": {
#                 "links": {
#                     "related": "/posts/1/comments/"
#                 }
#                 "data": [
#                     {"id": 5, "type": "comments"},
#                     {"id": 12, "type": "comments"}
#                 ],
#             }
#         },
#     }
# }

Installation

pip install marshmallow-jsonapi

Documentation

Full documentation is available at https://marshmallow-jsonapi.readthedocs.org/.

Requirements

  • Python >= 2.7 or >= 3.3

License

MIT licensed. See the bundled LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marshmallow-jsonapi-0.7.0.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

marshmallow_jsonapi-0.7.0-py2.py3-none-any.whl (13.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file marshmallow-jsonapi-0.7.0.tar.gz.

File metadata

File hashes

Hashes for marshmallow-jsonapi-0.7.0.tar.gz
Algorithm Hash digest
SHA256 04d5a1999d1f10b3398cea6d34554135eeb701a10a0019b15758cac9fb6e59b2
MD5 64269a52a6e6627c98266d16ace13b63
BLAKE2b-256 f4c1241f5d20d7a7031c25fe581e23fa5e974e238565544353a320448158bf64

See more details on using hashes here.

Provenance

File details

Details for the file marshmallow_jsonapi-0.7.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_jsonapi-0.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8d7a9b12abcbc7af396b31bb0b245c8e6635708e9ebf6aeeb346ef9d7dd8942f
MD5 ee837efa663a7db9aa564716b3040cd0
BLAKE2b-256 b108f92a3e26aa0f9fcdf306cfe2489b06203a9367a5d5ce9be5bf345aff4a00

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page