json2schema
Get (Guess) jsonschema from one json sample
Features
- json to jsonschema
Install
$ pip install json2schema
Use
Simple Use
from json2schema import json2schema
from pprint import pprint
data = {
"first_name": "George",
"last_name": "Washington",
"birthday": "1732-02-22",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}
schema = json2schema(data)
pprint(schema)
output
{'$schema': 'http://json-schema.org/schema',
'properties': {'address': {'properties': {'city': {'type': 'string'},
'country': {'type': 'string'},
'state': {'type': 'string'},
'street_address': {'type': 'string'}},
'required': ['street_address',
'city',
'state',
'country'],
'type': 'object'},
'birthday': {'type': 'string'},
'first_name': {'type': 'string'},
'last_name': {'type': 'string'}},
'required': ['first_name', 'last_name', 'birthday', 'address'],
'type': 'object'}
More arguments
You can use required_all=True to mark all properties or items as required
with schema = json2schema(data, required_all=True) you will get
{'$schema': 'http://json-schema.org/schema',
'properties': {'address': {'properties': {'city': {'type': 'string'},
'country': {'type': 'string'},
'state': {'type': 'string'},
'street_address': {'type': 'string'}},
'required': ['street_address',
'city',
'state',
'country'],
'type': 'object'},
'birthday': {'type': 'string'},
'first_name': {'type': 'string'},
'last_name': {'type': 'string'}},
'required': ['first_name', 'last_name', 'birthday', 'address'],
'type': 'object'}
or use check_value=True to mark all value exact the same with the sample data
with schema = json2schema(data, check_value=True), you will get
{'$schema': 'http://json-schema.org/schema',
'properties': {'address': {'properties': {'city': {'pattern': '^Mount Vernon$',
'type': 'string'},
'country': {'pattern': '^United '
'States$',
'type': 'string'},
'state': {'pattern': '^Virginia$',
'type': 'string'},
'street_address': {'pattern': '^3200 '
'Mount '
'Vernon '
'Memorial '
'Highway$',
'type': 'string'}},
'required': ['street_address',
'city',
'state',
'country'],
'type': 'object'},
'birthday': {'pattern': '^1732-02-22$', 'type': 'string'},
'first_name': {'pattern': '^George$', 'type': 'string'},
'last_name': {'pattern': '^Washington$', 'type': 'string'}},
'required': ['first_name', 'last_name', 'birthday', 'address'],
'type': 'object'}
TODO:
- get jsonschema with multiple samples
- more args for json2schema
Metadata
Release files for json2schema 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| json2schema-0.1.1.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| json2schema-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.4 kB
Release files / json2schema-0.1.1.tar.gz
| Download URL | json2schema-0.1.1.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
66efec8f389fcab2391810ce3bc6dcc32111c48a4d146c50d95be7b6b06fb860
|
|
BLAKE2b-256 checksum How to use checksums |
33062b98d8b35b3730f140822ecf5937386446e33d8c8a072724745b81f24618
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.6
|
Release files / json2schema-0.1.1-py3-none-any.whl
| Download URL | json2schema-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cb08d13893e8582f29a3b4f01b9c381742890c98d22b8dd7693cffb1f38047c2
|
|
BLAKE2b-256 checksum How to use checksums |
6783eabc118b2bd5e893a2e5a0b33a8c112ba540071f4c6dec714cc1e439e390
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.6
|