File::JSON::Slurper - slurp a JSON file into a data structure, and the reverse


 use File::JSON::Slurper qw/ read_json write_json /;

 my $ref = read_json('stuff.json');

 my $data = { name => 'NEILB', age => 21 };
 write_json('fibber.json', $data);


This module provides two functions for getting Perl data structures into and out of files in JSON format. One will read a Perl data structure from a JSON file, and the other will take a Perl data structure and write it to a file as JSON.

I wrote this module because I kept finding myself using File::Slurper to read JSON from a file, and then JSON::MaybeXS to convert the JSON to a Perl data structure.

No functions are exported by default, you must specify which function(s) you want to import.


read_json($filename, $encoding)

Read JSON from $filename and convert it to a Perl data structure. You'll get back either an arrayref or a hashref.

You can optionally specify the $encoding of the file, which defaults to UTF-8.

write_json($filename, $ref, $encoding)

Takes a Perl data structure $ref, converts it to JSON and then writes it to file $filename.

You can optionally specify an $encoding, which defaults to UTF-8.


JSON::Parse provides a function "json_file_to_perl" in JSON::Parse which is like the read_json function provided by this module. But you can't specify an encoding, and it doesn't provide a function for writing to a file as JSON.

File::Slurper is used to read and write files.

JSON::MaybeXS is used to encode and decode JSON. This itself is a front-end to the various JSON modules available on CPAN.



Neil Bowers <>


This software is copyright (c) 2016 by Neil Bowers <>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.