Skip to main content

Generate json text randomly

Project description

ranjg 0.5.0.x — Randomly json generator

ranjg is a package providing functions to generate random JSON data according to JSON-Schema-LIKE object. (It is similar to JSON schema, but does NOT support some keywords. Also see here.)

This package can be used on command line and python code.

Quick Start (on command line)

  1. Install the package with the following command. (Same to usage in python code)

    pip install ranjg
    
  2. Make a schema file. For example (schema.json):

    {
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "minLength": 1,
                "maxLength": 32
            },
            "age": {
                "type": "integer",
                "minimum": 0
            },
            "comment": {
                "type": "string",
                "minLength": 1
            }
        },
        "required": ["name", "age"]
    }
    
  3. Execute the package on command line with following command:

    python -m ranjg ./schema.json
    

    Then generated json string will be outputted.

    It generates JSON randomly. So you will get a different result each time you execute it.

Quick Start (in python code)

  1. Install the package with the following command. (Same to usage on command line)

    pip install ranjg
    
  2. Make a python file. For example (generate.py):

    import ranjg
    
    schema = {
        'type': 'object',
        'properties': {
            'name': {
                'type': 'string',
                'minLength': 1,
                'maxLength': 32
            },
            'age': {
                'type': 'integer',
                'minimum': 0
            },
            'comment': {
                'type': 'string',
                'minLength': 1
            }
        },
        'required': ['name', 'age']
    }
    
    generated = ranjg.gen(schema)
    
    print(generated)
    
  3. Execute the python code with following command:

    python generate.py
    

    Then generated json string will be outputted.

    It generates JSON randomly. So you will get a different result each time you execute it.

Document (command line usage)

You can execute ranjg with below command:

python -m ranjg <schema_file_path> [-j <json_output_path> [-n <num>] ] [--options <options_file>] [--list <multiplicity>]

This command generates a JSON string. Each argument has the following meaning:

  • <schema_file_path>: A file path of the JSON-schema-like file. Generated JSON string will be according to this schema. (What's "JSON-schema-like"?)
  • -j <json_output_path> (optional): When it's specified, a generated JSON string will be written to the specified file. When it's not specified, a generated JSON string will be written to stdout.
  • --options <options_file> (optional): It can be specified to use Options. See also Options.
  • --list <multiplicity> (optional): When it's specified, a list of length multiplicity is generated. Each element of the generated list specifies the schema.
  • -n <num> (optional): When it's specified, it repeats the generation num times and output each result to a different file. If you use this option, json_output_path must have placeholder such as {}.

Document (python code usage)

Usually, the following function is used:

def gen(schema: dict = None,
        schema_file: str = None,
        output_file: str = None,
        output_fp = None,
        options: Options = None,
        options_file: str = None) -> str

This function returns a generated JSON string.

Either schema or schema_file must be specified (generated JSON string is according to them). schema is a JSON-schema-like dict and schema_file is the path to a JSON-schema-like file. (What's "JSON-schema-like"?)

When output_file is specified, the generated JSON string will be written to the file of the specified path. When output_fp is specified with a file object opened in write mode, the result will be written to the specified file. You cannot specify output_file and output_fp in the same function call.

options or options_file can be specified to use Options. See also Options.

Supported keywords of schema

This package generates JSON string according to JSON-schema-LIKE object. JSON-schema-LIKE object is like general JSON schema, but it doesN'T SUPPORT some keywords of general JSON schema.

The following keywords can be used in much the same way as in regular JSON schema. (If you want to know the effect of each keyword, please read JSON Schema Reference.)

Supported keywords

  • "type"
    • Allowed: "object", "array", "string", "number", "integer", "boolean", "null" and multiple type.
  • "properties", "required"
  • "items", "minItems", "maxItems", "additionalItems"
  • "pattern", "minLength", "maxLength"
    • Warning: When "pattern" specified, "minLength" and "maxLength" are ignored.
  • "minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum"
  • "enum"

Options

Options allow you to specify generation rules that are not specified in the schema.

See also Options.

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

ranjg-0.5.0.1.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

ranjg-0.5.0.1-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file ranjg-0.5.0.1.tar.gz.

File metadata

  • Download URL: ranjg-0.5.0.1.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.1

File hashes

Hashes for ranjg-0.5.0.1.tar.gz
Algorithm Hash digest
SHA256 3572083244bb11556ea6e76d597aa3cbcc11de701931c4b7c0dff185b37079c6
MD5 2c843ccab1bc57cbadd546cd1329abe9
BLAKE2b-256 1c75b96e3f2951105cc0398ebdd136112d2946c7580df9c70aa4128bd4b151f8

See more details on using hashes here.

File details

Details for the file ranjg-0.5.0.1-py3-none-any.whl.

File metadata

  • Download URL: ranjg-0.5.0.1-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.1

File hashes

Hashes for ranjg-0.5.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ada6a7f90c94f804febdeb22c6278dd97794193f37e522df3747aaa71acfeb2b
MD5 86ea45a7288c434b9ce942456160bb33
BLAKE2b-256 25fc4f9ce674a814c5704fba8b0230613f6257702691f1a4d836870987c26f4b

See more details on using hashes here.

Supported by

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