https://github.com/hanzhichao/json2schema
JSON to JSONSchema
https://github.com/hanzhichao/json2schema
json jsonschema python
Last synced: 6 months ago
JSON representation
JSON to JSONSchema
- Host: GitHub
- URL: https://github.com/hanzhichao/json2schema
- Owner: hanzhichao
- License: mit
- Created: 2020-11-05T02:14:32.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-22T06:26:05.000Z (about 3 years ago)
- Last Synced: 2025-11-12T01:41:11.578Z (8 months ago)
- Topics: json, jsonschema, python
- Language: Python
- Homepage: https://pypi.org/project/json2schema/
- Size: 25.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json2schema
Get (Guess) jsonschema from one json sample

[](https://travis-ci.org/hanzhichao/json2schema)




## Features
- json to jsonschema
## Install
```
$ pip install json2schema
```
## Use
### Simple Use
```python
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
```shell
{'$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
```shell
{'$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
```shell
{'$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