So far the closest thing I've been able to find is warlock, which advertises this workflow:
Build your schema
>>> schema = {
'name': 'Country',
'properties': {
'name': {'type': 'string'},
'abbreviation': {'type': 'string'},
},
'additionalProperties': False,
}
Create a model
>>> import warlock
>>> Country = warlock.model_factory(schema)
Create an object using your model
>>> sweden = Country(name='Sweden', abbreviation='SE')
However, it's not quite that easy. The objects that Warlock produces lack much in the way of introspectible goodies. And if it supports nested dicts at initialization, I was unable to figure out how to make them work.
To give a little background, the problem that I was working on was how to take Chrome's JSONSchema API and produce a tree of request generators and response handlers. Warlock doesn't seem too far off the mark, the only downside is that meta-classes in Python can't really be turned into 'code'.
Other useful modules to look for:
- jsonschema - (which Warlock is built on top of)
- valideer - similar to jsonschema but with a worse name.
- bunch - An interesting structure builder thats half-way between a dotdict and construct
If you end up finding a good one-stop solution for this please follow up your question - I'd love to find one. I poured through github, pypi, googlecode, sourceforge, etc.. And just couldn't find anything really sexy.
For lack of any pre-made solutions, I'll probably cobble together something with Warlock myself. So if I beat you to it, I'll update my answer. :p
Answer from synthesizerpatel on Stack Overflow
ยป pip install json-schema-codegen
ยป pip install genson
validation - Tool to generate JSON schema from JSON data - Stack Overflow
Python JSON dummy data generation from JSON schema - Stack Overflow
Automatic fake JSON data creation from schema
Generating JSON Schemas Using Typed Objects (Similar to Python "Pydantic")
Videos
So far the closest thing I've been able to find is warlock, which advertises this workflow:
Build your schema
>>> schema = {
'name': 'Country',
'properties': {
'name': {'type': 'string'},
'abbreviation': {'type': 'string'},
},
'additionalProperties': False,
}
Create a model
>>> import warlock
>>> Country = warlock.model_factory(schema)
Create an object using your model
>>> sweden = Country(name='Sweden', abbreviation='SE')
However, it's not quite that easy. The objects that Warlock produces lack much in the way of introspectible goodies. And if it supports nested dicts at initialization, I was unable to figure out how to make them work.
To give a little background, the problem that I was working on was how to take Chrome's JSONSchema API and produce a tree of request generators and response handlers. Warlock doesn't seem too far off the mark, the only downside is that meta-classes in Python can't really be turned into 'code'.
Other useful modules to look for:
- jsonschema - (which Warlock is built on top of)
- valideer - similar to jsonschema but with a worse name.
- bunch - An interesting structure builder thats half-way between a dotdict and construct
If you end up finding a good one-stop solution for this please follow up your question - I'd love to find one. I poured through github, pypi, googlecode, sourceforge, etc.. And just couldn't find anything really sexy.
For lack of any pre-made solutions, I'll probably cobble together something with Warlock myself. So if I beat you to it, I'll update my answer. :p
python-jsonschema-objects is an alternative to warlock, build on top of jsonschema
python-jsonschema-objects provides an automatic class-based binding to JSON schemas for use in python.
Usage:
Sample Json Schema
schema = '''{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
},
"dogs": {
"type": "array",
"items": {"type": "string"},
"maxItems": 4
},
"gender": {
"type": "string",
"enum": ["male", "female"]
},
"deceased": {
"enum": ["yes", "no", 1, 0, "true", "false"]
}
},
"required": ["firstName", "lastName"]
} '''
Converting the schema object to class
import python_jsonschema_objects as pjs
import json
schema = json.loads(schema)
builder = pjs.ObjectBuilder(schema)
ns = builder.build_classes()
Person = ns.ExampleSchema
james = Person(firstName="James", lastName="Bond")
james.lastName
u'Bond' james
example_schema lastName=Bond age=None firstName=James
Validation :
james.age = -2 python_jsonschema_objects.validators.ValidationError: -2 was less or equal to than 0
But problem is , it is still using draft4validation while jsonschema has moved over draft4validation , i filed an issue on the repo regarding this . Unless you are using old version of jsonschema , the above package will work as shown.
Summarising the other answers, here are the JSON schema generators proposed so far:
Online:
- https://www.liquid-technologies.com/online-json-to-schema-converter (1 input)
- http://www.jsonschema.net (1 input)
- https://easy-json-schema.github.io (1 input)
- https://json.ophir.dev/ (1 input, nice UI)
Python:
- https://github.com/gonvaled/jskemator (1 input but allows iteration)
- https://github.com/perenecabuto/json_schema_generator (1 input)
- https://github.com/rnd0101/json_schema_inferencer (1 input I think)
- https://pypi.python.org/pypi/genson/ (multiple inputs)
- https://pypi.python.org/pypi/skinfer (multiple inputs)
NodeJS:
- https://github.com/Nijikokun/generate-schema (multiple inputs (pass object array))
- https://github.com/easy-json-schema/easy-json-schema (1 input)
- https://github.com/aspecto-io/genson-js (multiple inputs)
Ruby:
- https://github.com/maxlinc/json-schema-generator (1 input)
You might be looking for this:
http://www.jsonschema.net
It is an online tool that can automatically generate JSON schema from JSON string. And you can edit the schema easily.
A library that does exactly this is hypothesis-jsonschema
Hypothesis is a library that can generate arbitrary data that conforms to a given specification.
hypothesis-jsonschema makes it possible to convert JSON Schema into specifications that can be used by Hypothesis.
Here is an example showing a unit test written using Hypothesis and hypothesis-jsonschema:
from hypothesis import given
from hypothesis_jsonschema import from_schema
@given(from_schema({"type": "integer", "minimum": 1, "exclusiveMaximum": 10}))
def test_integers(value):
assert isinstance(value, int)
assert 1 <= value < 10
The most closest what I found within python is https://github.com/ueg1990/faker-schema, but it is not JSON-SCHEMA also there is nodejs implementation that is directly what you are searching for https://github.com/json-schema-faker/json-schema-faker
https://github.com/ghandic/jsf
Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.
Main Features
Provides out of the box data generation from any JSON schema ๐ฆ
Extendable custom data providers using any lambda functions ๐
Multi level state for dependant data (eg multiple objects sharing value, such as children with same surname) ๐ค
Inbuilt validation of fake JSON produced โ
In memory conversion from JSON Schema to Pydantic Models with generated examples ๐คฏ
Seamless integration with FastAPI ๐
Installation
$ pip install jsf ---> 100%
Usage
Basic ๐
from jsf import JSF
faker = JSF(
{
"type": "object",
"properties": {
"name": {"type": "string", "$provider": "faker.name"},
"email": {"type": "string", "$provider": "faker.email"},
},
"required": ["name", "email"],
}
)
fake_json = faker.generate()Results in ...
{
'name': 'Jesse Phillips',
'email': 'xroberson@hotmail.com'
}From JSON file ๐
from jsf import JSF
faker = JSF.from_json("demo-schema.json")
fake_json = faker.generate()<details markdown="1"> <summary>Or run stright from the <code>commandline</code>...</summary>
Native install
jsf --schema src/tests/data/custom.json --instance wow.json
Docker
docker run -v $PWD:/data challisa/jsf jsf --schema /data/custom.json --instance /data/example.json
</details>
FastAPI Integration ๐
Create a file main.py with:
from jsf import JSF
from fastapi import FastAPI
app = FastAPI(docs_url="/")
generator = JSF.from_json("custom.json")
@app.get("/generate", response_model=generator.pydantic())
def read_root():
return generator.generate()Run the server with:
<div class="termy">
$ uvicorn main:app --reload INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [28720] INFO: Started server process [28722] INFO: Waiting for application startup. INFO: Application startup complete.
Navigate to http://127.0.0.1:8000 and check out your endpoint. Notice the following are all automatically created:
Schema with descriptions and examples
Example response
Data generation by clicking "try it out"