The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

JSON::Schema::Generate - Generate JSON Schemas from data!

VERSION

Version 0.09

SYNOPSIS

        use JSON::Schema::Generate;

        my $data = '{
                "checked": false,
                "dimensions": {
                        "width": 10,
                        "height": 10
                },
                "id": 1,
                "name": "Opposite",
                "distance": 435,
                "tags": [
                        { "date-time": "2020-02-24T10:00:00+00:00" }
                ]
        }';

        my $schema = JSON::Schema::Generate->new(
                id => 'https://flat.world-wide.world/schema',
                title => '...'
                description => '...',
                spec => {
                        name => {
                                title => '...',
                                description => '...'
                        },
                        ...
                }
        )->learn($data)->generate;

        use JSON::Schema;
        my $validator = JSON::Schema->new($schema);
        my $result = $validator->validate($data);

        ...

        my $schema = JSON::Schema::Generate->new(
                no_id => 1
        )->learn($data)->generate(1);

        use JSON::Schema::Draft201909;

        $js = JSON::Schema::Draft201909->new;
        $result = $js->evaluate_json_string($data, $schema);

DESCRIPTION

JSON::Schema::Generate is a tool allowing you to derive JSON Schemas from a set of data.

SUBROUTINES/METHODS

new

Instantiate a new JSON::Schema::Generate Object

        my $schema = JSON::Schema->new(
                ...
        );

It accepts the following parameters:

id

The root $id of the schema. default: http://example.com/root.json

title

The root title of the schema. default: The Root Schema

description

The root description of the schema. default: The root schema is the schema that comprises the entire JSON document.

schema

The root schema version. default: 'http://json-schema.org/draft-07/schema#'

spec

A mapping hash reference that represent a key inside of the passed data and a value that contains additional metadata to be added to the schema. default: {}

merge_examples

Merge all learn data examples into a single example. default: false

none_required

Do not analyse required keys in properties. default: false.

no_id

Do not add $id(s) to properties and items. default: false.

learn

Accepts a JSON string, Hashref or ArrayRef that it will traverse to build a valid JSON schema. Learn can be chained allowing you to build a schema from multiple data sources.

        $schema->learn($data1)->learn($data2)->learn($data3);

generate

Compiles the learned data and generates the final JSON schema in JSON format.

        $schema->generate();

Optionally you can pass a boolean (true value) which will return the schema as a perl struct.

        $schema->generate(1)

EXAMPLE

        use JSON::Schema::Generate;

        my $data1 = '{
                "links" : {
                        "cpantesters_reports" : "http://cpantesters.org/author/L/LNATION.html",
                        "cpan_directory" : "http://cpan.org/authors/id/L/LN/LNATION",
                        "metacpan_explorer" : "https://explorer.metacpan.org/?url=/author/LNATION",
                        "cpantesters_matrix" : "http://matrix.cpantesters.org/?author=LNATION",
                        "cpants" : "http://cpants.cpanauthors.org/author/LNATION",
                        "backpan_directory" : "https://cpan.metacpan.org/authors/id/L/LN/LNATION"
                },
                "city" : "PLUTO",
                "updated" : "2020-02-16T16:43:51",
                "region" : "GHANDI",
                "is_pause_custodial_account" : false,
                "country" : "WO",
                "website" : [
                        "https://www.lnation.org"
                ],
                "asciiname" : "Robert Acock",
                "gravatar_url" : "https://secure.gravatar.com/avatar/8e509558181e1d2a0d3a5b55dec0b108?s=130&d=identicon",
                "pauseid" : "LNATION",
                "email" : [
                        "lnation@cpan.org"
                ],
                "release_count" : {
                        "cpan" : 378,
                        "backpan-only" : 34,
                        "latest" : 114
                },
                "name" : "Robert Acock"
        }';

        my $data2 = '{
                "asciiname" : "",
                "release_count" : {
                        "latest" : 56,
                        "backpan-only" : 358,
                        "cpan" : 190
                },
                "name" : "Damian Conway",
                "email" : "damian@conway.org",
                "is_pause_custodial_account" : false,
                "gravatar_url" : "https://secure.gravatar.com/avatar/3d4a6a089964a744d7b3cf2415f81951?s=130&d=identicon",
                "links" : {
                        "cpants" : "http://cpants.cpanauthors.org/author/DCONWAY",
                        "cpan_directory" : "http://cpan.org/authors/id/D/DC/DCONWAY",
                        "cpantesters_matrix" : "http://matrix.cpantesters.org/?author=DCONWAY",
                        "cpantesters_reports" : "http://cpantesters.org/author/D/DCONWAY.html",
                        "backpan_directory" : "https://cpan.metacpan.org/authors/id/D/DC/DCONWAY",
                        "metacpan_explorer" : "https://explorer.metacpan.org/?url=/author/DCONWAY"
                },
                "pauseid" : "DCONWAY",
                "website" : [
                        "http://damian.conway.org/"
                ]
        }';

        my $schema = JSON::Schema::Generate->new(
                id => 'https://metacpan.org/author.json',
                title => 'The CPAN Author Schema',
                description => 'A representation of a cpan author.',
        )->learn($data1)->learn($data2)->generate;

Will generate the following schema:

        {
                "$schema" : "http://json-schema.org/draft-07/schema#",
                "$id" : "https://metacpan.org/author.json",
                "title" : "The CPAN Author Schema",
                "description" : "A representation of a cpan author.",
                "type" : "object",
                "examples" : [
                        {
                                "region" : "GHANDI",
                                "gravatar_url" : "https://secure.gravatar.com/avatar/8e509558181e1d2a0d3a5b55dec0b108?s=130&d=identicon",
                                "is_pause_custodial_account" : false,
                                "asciiname" : "Robert Acock",
                                "release_count" : {
                                        "backpan-only" : 34,
                                        "latest" : 114,
                                        "cpan" : 378
                                },
                                "country" : "WO",
                                "city" : "PLUTO",
                                "pauseid" : "LNATION",
                                "links" : {
                                        "cpants" : "http://cpants.cpanauthors.org/author/LNATION",
                                        "metacpan_explorer" : "https://explorer.metacpan.org/?url=/author/LNATION",
                                        "cpantesters_reports" : "http://cpantesters.org/author/L/LNATION.html",
                                        "cpan_directory" : "http://cpan.org/authors/id/L/LN/LNATION",
                                        "cpantesters_matrix" : "http://matrix.cpantesters.org/?author=LNATION",
                                        "backpan_directory" : "https://cpan.metacpan.org/authors/id/L/LN/LNATION"
                                },
                                "updated" : "2020-02-16T16:43:51",
                                "website" : [
                                        "https://www.lnation.org"
                                ],
                                "name" : "Robert Acock",
                                "email" : [
                                        "lnation@cpan.org"
                                ]
                        },
                        {
                                "is_pause_custodial_account" : false,
                                "gravatar_url" : "https://secure.gravatar.com/avatar/3d4a6a089964a744d7b3cf2415f81951?s=130&d=identicon",
                                "asciiname" : "",
                                "release_count" : {
                                        "backpan-only" : 358,
                                        "latest" : 56,
                                        "cpan" : 190
                                },
                                "links" : {
                                        "cpan_directory" : "http://cpan.org/authors/id/D/DC/DCONWAY",
                                        "cpantesters_reports" : "http://cpantesters.org/author/D/DCONWAY.html",
                                        "cpants" : "http://cpants.cpanauthors.org/author/DCONWAY",
                                        "metacpan_explorer" : "https://explorer.metacpan.org/?url=/author/DCONWAY",
                                        "backpan_directory" : "https://cpan.metacpan.org/authors/id/D/DC/DCONWAY",
                                        "cpantesters_matrix" : "http://matrix.cpantesters.org/?author=DCONWAY"
                                },
                                "pauseid" : "DCONWAY",
                                "website" : [
                                        "http://damian.conway.org/"
                                ],
                                "email" : "damian@conway.org",
                                "name" : "Damian Conway"
                        }
                ],
                "required" : [
                        "asciiname",
                        "gravatar_url",
                        "is_pause_custodial_account",
                        "release_count",
                        "pauseid",
                        "links",
                        "name",
                        "email",
                        "website"
                ],
                "properties" : {
                        "asciiname" : {
                                "$id" : "#/properties/asciiname",
                                "title" : "The Asciiname Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "Robert Acock",
                                        ""
                                ]
                        },
                        "city" : {
                                "$id" : "#/properties/city",
                                "title" : "The City Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "PLUTO"
                                ]
                        },
                        "country" : {
                                "$id" : "#/properties/country",
                                "title" : "The Country Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "WO"
                                ]
                        },
                        "email" : {
                                "$id" : "#/properties/email",
                                "title" : "The Email Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : [
                                        "array",
                                        "string"
                                ],
                                "items" : {
                                        "$id" : "#/properties/email/items",
                                        "title" : "The Items Schema",
                                        "description" : "An explanation about the purpose of this instance.",
                                        "type" : "string",
                                        "examples" : [
                                                "lnation@cpan.org"
                                        ]
                                },
                                "examples" : [
                                        "damian@conway.org"
                                ]
                        },
                        "gravatar_url" : {
                                "$id" : "#/properties/gravatar_url",
                                "title" : "The Gravatar_url Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "https://secure.gravatar.com/avatar/8e509558181e1d2a0d3a5b55dec0b108?s=130&d=identicon",
                                        "https://secure.gravatar.com/avatar/3d4a6a089964a744d7b3cf2415f81951?s=130&d=identicon"
                                ]
                        },
                        "is_pause_custodial_account" : {
                                "$id" : "#/properties/is_pause_custodial_account",
                                "title" : "The Is_pause_custodial_account Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "boolean",
                                "examples" : [
                                        true,
                                        false
                                ]
                        },
                        "links" : {
                                "$id" : "#/properties/links",
                                "title" : "The Links Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "object",
                                "examples" : [
                                        {
                                                "cpants" : "http://cpants.cpanauthors.org/author/LNATION",
                                                "metacpan_explorer" : "https://explorer.metacpan.org/?url=/author/LNATION",
                                                "cpantesters_reports" : "http://cpantesters.org/author/L/LNATION.html",
                                                "cpan_directory" : "http://cpan.org/authors/id/L/LN/LNATION",
                                                "cpantesters_matrix" : "http://matrix.cpantesters.org/?author=LNATION",
                                                "backpan_directory" : "https://cpan.metacpan.org/authors/id/L/LN/LNATION"
                                        },
                                        {
                                                "cpan_directory" : "http://cpan.org/authors/id/D/DC/DCONWAY",
                                                "cpantesters_reports" : "http://cpantesters.org/author/D/DCONWAY.html",
                                                "cpants" : "http://cpants.cpanauthors.org/author/DCONWAY",
                                                "metacpan_explorer" : "https://explorer.metacpan.org/?url=/author/DCONWAY",
                                                "backpan_directory" : "https://cpan.metacpan.org/authors/id/D/DC/DCONWAY",
                                                "cpantesters_matrix" : "http://matrix.cpantesters.org/?author=DCONWAY"
                                        }
                                ],
                                "required" : [
                                        "cpantesters_matrix",
                                        "backpan_directory",
                                        "metacpan_explorer",
                                        "cpants",
                                        "cpantesters_reports",
                                        "cpan_directory"
                                ],
                                "properties" : {
                                        "backpan_directory" : {
                                                "$id" : "#/properties/links/properties/backpan_directory",
                                                "title" : "The Backpan_directory Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "string",
                                                "examples" : [
                                                        "https://cpan.metacpan.org/authors/id/L/LN/LNATION",
                                                        "https://cpan.metacpan.org/authors/id/D/DC/DCONWAY"
                                                ]
                                        },
                                        "cpan_directory" : {
                                                "$id" : "#/properties/links/properties/cpan_directory",
                                                "title" : "The Cpan_directory Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "string",
                                                "examples" : [
                                                        "http://cpan.org/authors/id/L/LN/LNATION",
                                                        "http://cpan.org/authors/id/D/DC/DCONWAY"
                                                ]
                                        },
                                        "cpantesters_matrix" : {
                                                "$id" : "#/properties/links/properties/cpantesters_matrix",
                                                "title" : "The Cpantesters_matrix Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "string",
                                                "examples" : [
                                                        "http://matrix.cpantesters.org/?author=LNATION",
                                                        "http://matrix.cpantesters.org/?author=DCONWAY"
                                                ]
                                        },
                                        "cpantesters_reports" : {
                                                "$id" : "#/properties/links/properties/cpantesters_reports",
                                                "title" : "The Cpantesters_reports Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "string",
                                                "examples" : [
                                                        "http://cpantesters.org/author/L/LNATION.html",
                                                        "http://cpantesters.org/author/D/DCONWAY.html"
                                                ]
                                        },
                                        "cpants" : {
                                                "$id" : "#/properties/links/properties/cpants",
                                                "title" : "The Cpants Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "string",
                                                "examples" : [
                                                        "http://cpants.cpanauthors.org/author/LNATION",
                                                        "http://cpants.cpanauthors.org/author/DCONWAY"
                                                ]
                                        },
                                        "metacpan_explorer" : {
                                                "$id" : "#/properties/links/properties/metacpan_explorer",
                                                "title" : "The Metacpan_explorer Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "string",
                                                "examples" : [
                                                        "https://explorer.metacpan.org/?url=/author/LNATION",
                                                        "https://explorer.metacpan.org/?url=/author/DCONWAY"
                                                ]
                                        }
                                }
                        },
                        "name" : {
                                "$id" : "#/properties/name",
                                "title" : "The Name Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "Robert Acock",
                                        "Damian Conway"
                                ]
                        },
                        "pauseid" : {
                                "$id" : "#/properties/pauseid",
                                "title" : "The Pauseid Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "LNATION",
                                        "DCONWAY"
                                ]
                        },
                        "region" : {
                                "$id" : "#/properties/region",
                                "title" : "The Region Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "GHANDI"
                                ]
                        },
                        "release_count" : {
                                "$id" : "#/properties/release_count",
                                "title" : "The Release_count Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "object",
                                "examples" : [
                                        {
                                                "backpan-only" : 34,
                                                "latest" : 114,
                                                "cpan" : 378
                                        },
                                        {
                                                "backpan-only" : 358,
                                                "latest" : 56,
                                                "cpan" : 190
                                        }
                                ],
                                "required" : [
                                        "latest",
                                        "backpan-only",
                                        "cpan"
                                ],
                                "properties" : {
                                        "backpan-only" : {
                                                "$id" : "#/properties/release_count/properties/backpan-only",
                                                "title" : "The Backpan-only Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "integer",
                                                "examples" : [
                                                        34,
                                                        358
                                                ]
                                        },
                                        "cpan" : {
                                                "$id" : "#/properties/release_count/properties/cpan",
                                                "title" : "The Cpan Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "integer",
                                                "examples" : [
                                                        378,
                                                        190
                                                ]
                                        },
                                        "latest" : {
                                                "$id" : "#/properties/release_count/properties/latest",
                                                "title" : "The Latest Schema",
                                                "description" : "An explanation about the purpose of this instance.",
                                                "type" : "integer",
                                                "examples" : [
                                                        114,
                                                        56
                                                ]
                                        }
                                }
                        },
                        "updated" : {
                                "$id" : "#/properties/updated",
                                "title" : "The Updated Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "string",
                                "examples" : [
                                        "2020-02-16T16:43:51"
                                ]
                        },
                        "website" : {
                                "$id" : "#/properties/website",
                                "title" : "The Website Schema",
                                "description" : "An explanation about the purpose of this instance.",
                                "type" : "array",
                                "items" : {
                                        "$id" : "#/properties/website/items",
                                        "title" : "The Items Schema",
                                        "description" : "An explanation about the purpose of this instance.",
                                        "type" : "string",
                                        "examples" : [
                                                "https://www.lnation.org",
                                                "http://damian.conway.org/"
                                        ]
                                }
                        }
                }
        }

AUTHOR

LNATION, <email at lnation.org>

BUGS

Please report any bugs or feature requests to bug-json-schema-generate at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Schema-Generate. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc JSON::Schema::Generate

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

This software is Copyright (c) 2020->2021 by LNATION.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)