JSON to Perl Converter

Turn JSON into a Perl data structure you can use or require

JSON Input

Perl Output

Paste your JSON on the left and the Perl code shows up here.

Or drop a file on this box, , or .

What does the JSON to Perl converter do?

If you want JSON data baked into a Perl script (test fixtures, a lookup table, default config), converting it by hand is fiddly and easy to get wrong. Paste JSON here and you get a small module: package RootObject;, use strict; and use warnings;, then our $data = { ... }; and the closing 1;. Objects become hash references, arrays become array references and null becomes undef, as described in perldata.

Strings are where hand conversion usually breaks. An email address in double quotes makes Perl try to interpolate @example, and use strict refuses to compile it. This converter writes strings and keys in single quotes, escaping only \ and ', so $5, @ and backslashes come through unchanged. Only strings containing newlines or other control characters switch to double quotes, and then $ and @ are escaped too.

Integers that fit in 64 bits, ICCIDs and snowflake IDs included, are written as exact bare literals, which a 64-bit perl holds without rounding. Anything larger stays a quoted string, which keeps every digit intact; reach for Math::BigInt if you need to do math on it. Booleans are written as 1 and 0. Because $data is declared with our, other code can read it as $RootObject::data after a require. It all runs in your browser.

How to use it

The Perl code updates as you type.

1

Paste JSON

Paste or type into JSON Input, load a .json or .txt through Upload, or click Sample. An invoice with one nested line item shows how hash and array references nest:

{"invoiceNo":"INV-2291","customer":"Maeve Doyle","total":1840.50,"paid":false,"lines":[{"sku":"SKU-101","qty":3}]}

Package Name sets the package line and the download file name; My::Fixtures works. Characters Perl doesn't allow in a package name are replaced with underscores.

2

Check the data structure

The Perl Output panel shows the module with two-space indentation. Every value keeps its JSON type: numbers stay numbers, strings stay quoted, even when they only contain digits.

3

Copy or download

Copy it or Download a .pm file (for My::Fixtures that's Fixtures.pm). Put it on @INC, require My::Fixtures; and read $My::Fixtures::data.

Where this saves time

Test fixtures

Paste an API response and use the hashref as expected data in a Test::More test, instead of keeping a separate JSON file and decoding it in every test.

Lookup tables in scripts

Country codes, SKU-to-price maps or feature flags exported as JSON can sit at the top of a script as a plain hash, with no JSON module required at runtime.

Legacy CGI and cron jobs

Older boxes don't always have JSON::XS installed. A pre-converted data file loads with nothing but core Perl.

Checking what a decoder returns

Modules like JSON::PP build the same nested references at runtime. Converting a sample shows the structure you'll be walking.

Common Questions

Is the output safe with emails and dollar signs?

Yes. Strings are single-quoted, so ava.chen@example.com and $5 are taken literally. Strings with newlines use double quotes with $, @ and \ escaped.

How are true and false written?

As 1 and 0, which is how most Perl code tests truth. If you need real boolean objects, decode the JSON at runtime with JSON::PP instead.

Will big numbers survive?

Integers up to 64 bits are written exactly and read back exactly by a 64-bit perl. Larger integers become strings, with a comment explaining why. Strings that merely look like numbers stay strings.

Why does it use our instead of my?

A my variable at file scope can't be seen from outside the file, so require would give you nothing. With our, the data is reachable as $PackageName::data.

Is my JSON uploaded?

No. The .pm text is assembled in your browser, so the invoice or config you paste stays on your machine. Input above 256 KB gets a message instead of a module; for a blob that size, ship the .json file and decode it with JSON::PP when the script runs.

Other JSON Tools You Might Need

Need the same data for another language or format?