Skip to main content

JSON API to document parser

Project description

https://img.shields.io/pypi/v/json-api-doc.svg https://img.shields.io/travis/noplay/json-api-doc.svg Documentation Status Updates

This library provides ability to transform between normalized JSON API (http://jsonapi.org/) documents and denormalized Python dictionary object for easier manipulation in code. Also available as a command line utility and Python 3 module.

Deserialization

For this JSON API document:

{
  "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"
      }
    }
  ]
}

The simplified version will be:

[
    {
        "type": "articles",
        "id": "1",
        "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",
        "author": {
            "type": "people",
            "id": "42",
            "name": "John",
            "age": 80,
            "gender": "male"
        }
    }
]

Serialization

To turn an dict into JSON API specification document the root of your object must contain a $type key with a value corresponding to the name of the object’s resource type. Any sub-dict or sub-array of dicts that also contain a $type key will be considered an included documents and serialized accordingly.

[
    {
        "$type": "articles",
        "id": "1",
        "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",
        "author": {
            "$type": "people",
            "id": "42",
            "name": "John",
            "age": 80,
            "gender": "male"
        }
    }
]
{
  "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"
      }
    }
  ]
}

Usage as python module

import json_api_doc

document =  {
    'data': {
        'type': 'article',
        'id': '1',
        'attributes': {
            'name': 'Article 1'
        }
    }
}
json_api_doc.deserialize(document)
import json_api_doc

document =  {
  '$type': 'article',
  'id': '1',
  'name': 'Article 1'
}
json_api_doc.serialize(document)

Usage as cli

$ jsonapidoc document.json

Contributors

Licence

Free software: Apache Software License 2.0

Documentation

Full Documentation is available: https://json-api-doc.readthedocs.io.

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

json-api-doc-0.15.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

json_api_doc-0.15.0-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file json-api-doc-0.15.0.tar.gz.

File metadata

  • Download URL: json-api-doc-0.15.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.7

File hashes

Hashes for json-api-doc-0.15.0.tar.gz
Algorithm Hash digest
SHA256 1cbbf34d04b7768bf0de187363333a1c96294588e778f722a98ef60765458a9c
MD5 f4637f1fb1de0ba31e8f4594ab59a157
BLAKE2b-256 825ff2fea5059a8e60e38712eafbfda3fd254ca8f2c44c3b5b3dd94c398ae672

See more details on using hashes here.

File details

Details for the file json_api_doc-0.15.0-py2.py3-none-any.whl.

File metadata

  • Download URL: json_api_doc-0.15.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.7

File hashes

Hashes for json_api_doc-0.15.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4a9abc840c8706b17057be8e99958821a133040e891bdb5beac0abe045ab6b1d
MD5 b44b7e81ea14b8079b3b012d9a368fd1
BLAKE2b-256 4b3033f9ccb7e265ff98e17e1662d2871322ea7adbc73d9c8c69ef4d9d5605a7

See more details on using hashes here.

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