jsf: Fake JSON from Schema
JSF (JSON Schema Faker) is a Python library designed to generate realistic-looking fake JSON data based on a given JSON schema. It supports a wide range of JSON schema features, including types, formats, patterns, and more complex structures like arrays and objects. The library is actively maintained with frequent releases, with the current stable PyPI version being 0.11.2, although more recent releases like 0.11.4 have appeared on GitHub, indicating a rapid development pace.
Warnings
- breaking Version 0.8.0 introduced a breaking change by switching its internal dependency to Pydantic v2. Projects using Pydantic v1.x alongside `jsf` will need to upgrade Pydantic to v2.x or pin `jsf` to a version prior to 0.8.0.
- gotcha Starting from version 0.11.2, `jsf` supports options to `prefer_default` and `prefer_examples` during generation. If your schema includes `default` or `examples` keywords and you were previously relying on random generation for these fields, your generated data might change if these new options are enabled, potentially leading to less varied data.
- gotcha Prior to version 0.10.0, non-required fields in a schema might not have been generated by default. Version 0.10.0 introduced the `force_non_required_fields` option, which, when enabled, ensures that even optional fields are always generated. Users upgrading might notice a change in data sparsity if they expected optional fields to sometimes be omitted.
- gotcha In versions prior to 0.11.0, `jsf` had limited support for recursive schemas and enums of objects, which could lead to errors or incorrect generation. These issues were addressed in version 0.11.0.
Install
-
pip install jsf
Imports
- resolve
import jsf result = jsf.resolve(schema)
- JSF
from jsf import JSF generator = JSF(schema) result = generator.generate()
Quickstart
import jsf
import os
# Example JSON schema
schema = {
"type": "object",
"properties": {
"id": {"type": "string", "format": "uuid"},
"name": {"type": "string", "pattern": "^[A-Za-z ]+$"},
"age": {"type": "integer", "minimum": 18, "maximum": 99},
"email": {"type": "string", "format": "email"},
"isActive": {"type": "boolean"},
"tags": {"type": "array", "items": {"type": "string"}, "minItems": 1, "maxItems": 3}
},
"required": ["id", "name", "age", "email", "isActive"]
}
# Generate a single fake JSON object
fake_data = jsf.resolve(schema)
print(fake_data)
# To demonstrate with a custom generator for multiple items or specific options
# from jsf import JSF
# generator = JSF(schema)
# print(generator.generate(prefer_default=True)) # Example of using a generator with options