using the JSON perl mod

I just thought I'd make a quick blog post on how to use the JSON perl mod. Why use JSON when we have XML, I'll leave that to Russ or Richard, but to make a long story short, easier object handling for the projected javascript driven DVLF.
So, the perl JSON module is actually very easy and nice to use, it will convert your perl data structure into JSON without a sweat.
Here's a quick example which I hope will be useful :

use strict;
use warnings;
use JSON;

my %hash;
foreach my $file (@list_of_files)  {
        my @list;
        while (<FILE>) {
        %hash{$file} = [@array];    # store array reference in hash

my $obj = \%results; 
my $json = new JSON;
my $js = $json-&gt;encode($obj, {pretty =&gt; 1, indent =&gt; 2}); # convert Perl data structure to JSON representation
$output .= "$js\n\n";
print $output;

And done!
Next PostNewer Post Previous PostOlder Post Home


  1. Ok, obviously there's a instead of () in the while loop, but Blogger keeps interpreting it as HTML :-)

  2. OK, once again FILE in between angled brackets