Skip to main content

Converts a JSON Schema to a Plain Old Python Object class

Project description

JSONSchema2PoPo2

Forked from github.com/frx08/jsonschema2popo

A converter to extract 'Plain Old Python Object' classes from JSON Schema files. Similar to the Java project JSONSchema2PoJo. Currently compatible with Python 3.4+ (when using enums, otherwise any version should be fine).

Installation

pip install jsonschema2popo2

Usage

Basic:

jsonschema2popo2 -o /path/to/output_file.py /path/to/json_schema.json

Options:

  • -t, --use-types - Add MyPy typings.
  • -ct, --constructor-type-check - Validate provided types in constructor. Default only type checks when setting property values and not when setting them in the constructor.
  • -s, --use-slots - Add a __slots__ to each generated class to be more memory efficient.
  • --no-generate-from-definitions - Don't generate any classes from the "definitions" section of the schema.
  • --no-generate-from-root-object - Don't generate any classes from the root of the schema.
  • -tp, --translate-properties - Translate property names to be snake_case. With this enabled, inner classes will no longer be prefixed by "_" since their names won't collide with the property name.

Encode Generated Object to JSON:

import json

g = GeneratedClass()
json.dumps(g.as_dict())

Decode JSON into Generated Object:

import json
g = GeneratedClass.from_dict(json.loads(data))

JSON Schema Format

This library is largely compatible with JSONSchema2PoJo and how that library reads the JSON Schema to generate Java. Specifically, for enumerations, this library supports setting the javaEnumNames array in JSON Schema for an enum to give names to the enum values that will be generated in the Python output.

Project details


Download files

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

Source Distribution

JSONSchema2PoPo2-2.0.14.tar.gz (13.8 kB view details)

Uploaded Source

File details

Details for the file JSONSchema2PoPo2-2.0.14.tar.gz.

File metadata

  • Download URL: JSONSchema2PoPo2-2.0.14.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.1.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.4

File hashes

Hashes for JSONSchema2PoPo2-2.0.14.tar.gz
Algorithm Hash digest
SHA256 a8230dcb438f98e1bf374dfd4d4b25f792230b00925aa5ab31048ef09bccda0e
MD5 61d05e31403377568278f2ce990650fd
BLAKE2b-256 40fc6eb3c01736c6e9c7c9a4c552a7ffeb0ab391f302cf9c965808e33581d05d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page