JSON to Perl Class Generator

Generate Perl Moose or Moo class definitions with type constraints from JSON data.

What is JSON to Perl Class Generator?

Perl remains a workhorse for system administration, ETL pipelines, and legacy web applications. The Moose object system brings modern OOP to Perl with type constraints, method modifiers, and role composition, while Moo provides a lighter-weight alternative with most of the same features. This tool inspects your JSON data and generates complete Moose or Moo class definitions with typed attributes. JSON strings become Str, integers become Int, floats become Num, booleans become Bool, null becomes Maybe[Item], and arrays become ArrayRef[T] with the element type inferred from the first item. Nested JSON objects are extracted into separate Perl packages under a configurable namespace prefix, following the standard Perl convention of organizing modules with :: separators. Each class includes properly structured has declarations with isa type constraints and is accessor specifications.

How to Use

  1. Set the package namespace prefix (e.g., MyApp::) to organize generated classes under a common namespace
  2. Choose between read-only (ro) and read-write (rw) accessor styles for all attributes
  3. Select Moose for full-featured OOP or Moo for a lightweight alternative
  4. Paste your JSON object into the input area and click "Generate"
  5. Copy the output into separate .pm files in your Perl project lib directory

Why Use This Tool?

Automatically infer Moose type constraints from JSON values
Generate Moose or Moo class definitions
Handle nested objects as separate packages
Support for required attributes and default values
Proper Perl package structure with namespace::autoclean

Tips & Best Practices

  • Choose Moose when you need method modifiers (before, after, around), role composition, or metaclass introspection
  • Choose Moo when startup speed is critical — Moo avoids Moose's metaclass overhead while providing the same attribute syntax
  • Use read-only (ro) accessors for data transfer objects and read-write (rw) for mutable domain objects
  • The Maybe[Item] type for null values is intentionally broad — replace Item with the concrete type (e.g., Maybe[Str]) for stricter validation
  • Place each generated package in its own .pm file following Perl convention: MyApp::Root goes in lib/MyApp/Root.pm

Frequently Asked Questions

How are JSON types mapped to Perl Moose types?

JSON strings map to Str, integers to Int, floats to Num, booleans to Bool, null to Maybe[Item], arrays to ArrayRef[Type] or ArrayRef for empty arrays, and nested objects become separate Perl packages referenced by name in the type constraint.

When should I avoid using this generator?

Skip this tool if your JSON contains self-referential or circular structures, since Perl packages cannot reference each other in a way that resolves circular dependencies. Also avoid it when you need MooseX::Types or Type::Library custom type libraries — the generated code uses inline type strings, not type library references.

What is the difference between Moose and Moo?

Moose is a full-featured postmodern object system for Perl 5 with metaclass protocol, type coercion, and method modifiers. Moo is a lightweight alternative that provides most of the same features with less overhead. Moo does not support namespace::autoclean or meta->make_immutable.

How are nested objects handled?

Nested JSON objects are converted to separate Perl packages with their own attribute definitions. They are ordered children-first so that referenced packages are defined before the packages that use them. The parent package references the nested package by name in its type constraint.

What does the package prefix do?

The package prefix sets the namespace for generated packages. For example, with prefix "MyApp::", a "Root" class becomes "MyApp::Root". This follows Perl convention of organizing modules under a namespace hierarchy using "::" as the separator.

Is my data sent to a server?

No. All type inference and code generation happens entirely in your browser. Your JSON data is never transmitted to any server, so it is safe to paste payloads containing credentials, internal API responses, or other sensitive values.

Real-world Examples

Generating Perl classes for a REST API client

When building a Perl client for a REST API, paste a sample JSON response to generate Moose classes with typed attributes. Each class can then be used with a JSON decoder like MooseX::Storage or Cpanel::JSON::XS to deserialize API responses into proper Perl objects.

Input
{
  "id": 1,
  "name": "Alice",
  "email": "[email protected]",
  "is_active": true,
  "tags": ["developer"]
}
Output
package MyApp::Tag;
use Moose;
use namespace::autoclean;

__PACKAGE__->meta->make_immutable;
1;

package MyApp::Root;
use Moose;
use namespace::autoclean;

has 'id' => (is => 'ro', isa => 'Int', required => 1);
has 'name' => (is => 'ro', isa => 'Str', required => 1);
has 'email' => (is => 'ro', isa => 'Str', required => 1);
has 'is_active' => (is => 'ro', isa => 'Bool', required => 1);
has 'tags' => (is => 'ro', isa => 'ArrayRef[Str]', required => 1);

__PACKAGE__->meta->make_immutable;
1;

Creating configuration object classes from JSON config files

Many Perl applications read configuration from JSON files. Paste your config JSON to generate Moo classes that validate the configuration structure at load time, catching missing keys and type mismatches before they cause runtime errors deep in the application logic.

Input
{
  "database": {
    "host": "localhost",
    "port": 5432,
    "name": "myapp"
  },
  "logging": {
    "level": "info",
    "path": "/var/log/myapp.log"
  }
}
Output
package MyApp::Database;
use Moo;

has 'host' => (is => 'ro', isa => 'Str', required => 1);
has 'port' => (is => 'ro', isa => 'Int', required => 1);
has 'name' => (is => 'ro', isa => 'Str', required => 1);

1;

package MyApp::Logging;
use Moo;

has 'level' => (is => 'ro', isa => 'Str', required => 1);
has 'path' => (is => 'ro', isa => 'Str', required => 1);

1;

package MyApp::Root;
use Moo;

has 'database' => (is => 'ro', isa => 'Database', required => 1);
has 'logging' => (is => 'ro', isa => 'Logging', required => 1);

1;

Related Tools