Skip to main content

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

Project description

Latest version Travis-CI

Homepage: http://marshmallow-jsonapi.readthedocs.io/

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.io/.

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.1.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

marshmallow_jsonapi-0.7.1-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.1.tar.gz.

File metadata

File hashes

Hashes for marshmallow-jsonapi-0.7.1.tar.gz
Algorithm Hash digest
SHA256 d5831d707f421b5f2f201bbb83f375ab2bb398f100b2f3d2e65ea6ec8ac43c10
MD5 20c57b89f91ecd9855e32b0838c26ed4
BLAKE2b-256 6d04c25ce747290a176d15746399cb426bf943f3127d6365e291c5ac67902847

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for marshmallow_jsonapi-0.7.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3404ca7517e95ca8ed5ec4a7303ba5115bd516397b7522f80b74b5f71df0f775
MD5 154bc8eea0e5977e5413f09f4f848904
BLAKE2b-256 696cf31fa5bc47d5a328b8f7191306c8cf9d1ad988ea6bd1b62a6a48dde5535b

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