Skip to main content

Generate code from JSON schema files.

Project description

Build Status


Generate code from JSON schema files.

Table of contents


This is a command line tool to take a json-schema file and generate code automatically.

For instance this json-schema definition:

  "$schema": "",
  "title": "Test",
  "type": "object",
  "properties": {
    "id": { "type": "integer" }

will generate this Python code:

class Test(object):
    def __init__(self, data=None):
        data = data or {} = data.get("id")

or this JavaScript+Flow code:

export class Test {
  id: ?number;

  constructor(data: Object = {}) { =;

Currently this tool generates code for Python and JavaScript with Flow annotations but it can be extended to generate code for any language.

The code generation is divided in two stages:

  1. generate the AST for the target language from the json-schema file
  2. convert the AST into the target language

This allows the tool to be language agnostic, that is it just needs to generate the AST in JSON format for the target language and then a language specific tool will convert this AST into proper code.

Currently supported languages

List of currently supported languages:

  • Python 2.7+
  • JavaScript ES7+ with Flow annotations
  • pure Flow annotations


  • Python 3.6 / 3.7
  • Node v12


Until this pull request in setuptools is fixed, the only way to install json-schema-codegen is to clone the repo:

git clone


usage: [-h] [--prefix PREFIX] [--language LANGUAGE]
                       [--output OUTPUT]

positional arguments:
  schema                Definition of the PRD as JSON schema

optional arguments:
  -h, --help            show this help message and exit
  --prefix PREFIX, -p PREFIX
                        Optional prefix for generated classes
  --language LANGUAGE, -l LANGUAGE
                        Output language. Default is python
  --output OUTPUT, -o OUTPUT
                        Output filename for the generated code

Code generation

Python 2

The generation of Python's code is integrated into the tool so it needs just a single invocation:

json_codegen --language python --output <output_py_file> <json-schema>

Python 3

The egenerator of pure Python 3 compatible code:

json_codegen --language python3 --output <output_py_file> <json-schema>

Python 3+Marshmallow

The generation of Python 3's code with Marshmallow support is integrated into the tool so it needs just a single invocation:

json_codegen --language python3+marshmallow --output <output_py_file> <json-schema>

JavaScript+Flow and Flow

Generating JavaScript+Flow and Flow code involves two steps, generating the AST:

json_codegen --language [javascript+flow|flow] --output <output_ast_json> <json-schema>

and generating the code from the AST:

bin/ast_to_js <output_ast_json> <output_js_file>

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for json-codegen, version 0.4.6
Filename, size File type Python version Upload date Hashes
Filename, size json_codegen-0.4.6.tar.gz (128.8 kB) File type Source Python version None Upload date Hashes View
Filename, size json_codegen-0.4.6-py3-none-any.whl (137.5 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page