Skip to main content

JSON Tools Library

Project description

JSON related tools

Infer Schema

With function infer_schema, you can get schema from json object. The output complys to JSON Schema Specification. Here is an example:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from json_tools2 import infer_schema
import json

def main():
    schema = infer_schema({"foo": 1})
    print(json.dumps(schema, indent=4))

if __name__ == '__main__':
    main()

Output

{
    "type": "object",
    "properties": {
        "foo": {
            "type": "integer"
        }
    }
}

When you have an array of element from different types, or object with the same property but from different type, we need to merge schema.

case 1: "a" and 2 are of different types and we need to merge schema

["a", 2]

case 2: "a" and 2 are of different types and we need to merge schema

[{"foo": "a"}, {"foo": 2}]

Following rules applies when we do schema merging:

  • For null, any type can merged to it and return a nullable type, for example, sting merge to null and return a nullable string
  • For string, only null can merged to string and return nullable string.
  • For number, followings are the only valid type that can merge to it
    • integer can merged to it without changing it
    • null can merge to it and become nullable number
  • For integer, followings are the only valid type that can merge to it
    • number can merged to it and upgrade it to number
    • null can merge to it and become nullable number
  • For boolean, followings are the only valid type that can merge to it
    • null can merge to it and become nullable boolean
  • For object, followings are the only valid type that can merge to it
    • null can merge to it and become nullable object
  • For array, followings are the only valid type that can merge to it
    • null can merge to it and become nullable array

We use anyOf to represent nullable type, for example: This represent a nullable integer type.

{
    "anyOf": [{"type": "integer"},{"type": "null"}]
}

For any schema inferred, you should be able to validate the payload with the schema inferred, here is an example:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# You need to do "pip install jsonschema"
from json_tools2 import infer_schema
from jsonschema import validate
import json


def main():
    payload = {"foo": 1}
    schema = infer_schema(payload)
    validate(instance=payload, schema=schema) # it should not throw exception

if __name__ == '__main__':
    main()

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-tools2-0.0.7.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distribution

json_tools2-0.0.7-py2.py3-none-any.whl (8.6 kB view hashes)

Uploaded Python 2 Python 3

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