Skip to main content

Build Status

json-schema-codegen

Generate code from JSON schema files.

Table of contents

Introduction

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

For instance this json-schema definition:

{
  "$schema": "http://json-schema.org/draft-04/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 {}

        self.id = data.get("id")

or this JavaScript+Flow code:

export class Test {
  id: ?number;

  constructor(data: Object = {}) {
    this.id = data.id;
  }
}

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 3.7+
  • JavaScript ES7+ with Flow annotations
  • pure Flow annotations

Requirements

  • Python 3.6 / 3.7
  • Node v12

Installation

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

git clone https://github.com/expobrain/json-schema-codegen.git

Usage

usage: json_codegen.py [-h] [--prefix PREFIX] [--language LANGUAGE]
                       [--output OUTPUT]
                       schema

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 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>

Metadata

Release files for json-codegen 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-codegen 0.6.0
File Size Uploaded Provenance
json_codegen-0.6.0.tar.gz 128.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for json-codegen 0.6.0
File Interpreter ABI Platform
json_codegen-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 263.1 kB

Release files / json_codegen-0.6.0.tar.gz

Download URL json_codegen-0.6.0.tar.gz
Size 128.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d4bfb002f54e3f908a962f32bcec7d9d8c46315a6dc676a8435f71dd107fbada
BLAKE2b-256 checksum
How to use checksums
04f7178db4d0667ed93b4801b78ed64048b1aa2c211d4380fcc5c5b49bec9d6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / json_codegen-0.6.0-py3-none-any.whl

Download URL json_codegen-0.6.0-py3-none-any.whl
Size 134.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d6bc0d1d4dd8f7ca105b3d7ccbcfae2c90fe0f4c28cf5a20fc77b353d2259cc
BLAKE2b-256 checksum
How to use checksums
229a20a7bf41d8b0ce15d61038ca1ca4db7f41761afbbac385c5dedf9df4aee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for json-codegen 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page