Skip to content

Latest commit

 

History

History
111 lines (93 loc) · 2.96 KB

readme.md

File metadata and controls

111 lines (93 loc) · 2.96 KB

jsonapi-pydantic

JSON:API implementation with Pydantic.

PyPI License

Description

jsonapi-pydantic provides a suite of Pydantic models matching the JSON:API specification.

Install

$ pip install jsonapi-pydantic

Or use your python package manager.

Usage

Object with primary data:

from jsonapi_pydantic.v1_0 import TopLevel

external_data = {
    "data": [
        {
            "type": "articles",
            "id": "1",
            "attributes": {
                "title": "JSON:API paints my bikeshed!",
                "body": "The shortest article. Ever.",
                "created": "2015-05-22T14:56:29.000Z",
                "updated": "2015-05-22T14:56:28.000Z",
            },
            "relationships": {"author": {"data": {"id": "42", "type": "people"}}},
        }
    ],
    "included": [
        {"type": "people", "id": "42", "attributes": {"name": "John", "age": 80, "gender": "male"}}
    ],
}

top_level = TopLevel(**external_data)

print(top_level.model_dump(exclude_unset=True))
"""
{
    "data": [
        {
            "type": "articles",
            "id": "1",
            "attributes": {
                "title": "JSON:API paints my bikeshed!",
                "body": "The shortest article. Ever.",
                "created": "2015-05-22T14:56:29.000Z",
                "updated": "2015-05-22T14:56:28.000Z",
            },
            "relationships": {"author": {"data": {"id": "42", "type": "people"}}},
        }
    ],
    "included": [
        {"type": "people", "id": "42", "attributes": {"name": "John", "age": 80, "gender": "male"}}
    ],
}
"""
print(top_level.data)
"""
[
    Resource(
        type="articles",
        id="1",
        attributes={
            "title": "JSON:API paints my bikeshed!",
            "body": "The shortest article. Ever.",
            "created": "2015-05-22T14:56:29.000Z",
            "updated": "2015-05-22T14:56:28.000Z",
        },
        relationships={
            "author": Relationship(
                links=None, data=ResourceIdentifier(id="42", type="people", meta=None), meta=None
            )
        },
        links=None,
        meta=None,
    )
]
"""

Examples

More examples can be found here.

License

See license.md.

Thank you

If you like this open source project, I'd really appreciate it if you put a star. Thank you!