Dispersion Design

< Back

Using JSON Data In Perl Scripts



After its introduction in the late 1990s, many people predicted that Extensible Markup Language (XML) would become the new universal standard for textual data exchange. However, as people started implementing applications to use XML, they quickly discovered the syntax to be too verbose and complicated to parse. As a result, many 'lightweight' formats appeared, one of those being JavaScript Object Notation (JSON).

JSON is based on standard JavaScript syntax, which makes it easy to read and debug for programmers already familiar with C, C++, Java or JavaScript. It’s simplicity has resulted in widespread adoption in many applications.

This article will explain how to read and write the JSON format when using the Perl programming language, to develop scripts and applications.

The JSON Format

The JSON format specification can be found at www.json.org. The following shows an example of a simple JSON file:

		"name" : "Tenth Doctor",
		"actor" : "David Tennant",
		"companions" : ["Rose", "Donna", "Martha"]
		"name" : "Eleventh Doctor",
		"actor" : "Matt Smith",
		"companions" : [ "Amy", "Rory", "River" ]

This file contains an array of two elements, each element being an object. Each object contains three elements: two strings ("name" and "actor") and a "companions" array. As you can see, the format is very flexible for entering complex data structures.

Getting The JSON Module

The fastest way to get started reading and writing JSON files with Perl is to use the JSON:PP module. The JSON::PP module (PP stands for Pure-Perl) is a core Perl module since Perl 5.14. If you do not have this module installed on your system, you can download JSON::PP from CPAN. Place the PP.pm file in a directory called "JSON" and make sure your Perl script knows where to find the module with the "use lib" command.

For example, if the PP.pm file is located at: "./modules/JSON/PP.pm", then you would add the following to your Perl script:

use lib './modules';
use JSON::PP;

Reading a JSON File

To read JSON information from an input stream, you first create a new JSON::PP object:

my $json = JSON::PP->new;

Then use the decode() method to read in the data:

my $data = $json->decode(<$json_stream>);

If you are reading the data from a file, you would also have to open and close the file:

my $filename = 'dr_who_data.json';
my $data;
if (open (my $json_stream, $filename))
	local $/ = undef;
	my $json = JSON::PP->new;
	$data = $json->decode(<$json_stream>);

The JSON data is read into a standard Perl data structure, so no special processing is required to access the data.

Using a JSON Array

JSON arrays are read into standard Perl arrays. After reading the following JSON data into Perl:

[ "Amy Pond", "Rory Williams", "River Song" ]

The data could be displayed with the following code:

foreach my $element (@$data)
	print "$element\n";

Which would result in the following:

Amy Pond
Rory Williams
River Song

Note that the $data variable is a reference to a Perl array, so it must be dereferenced (@$data) before iterating through the array.

Using a JSON Object

JSON objects are read into Perl hashes, so the following JSON object:

	"name" : "Tenth Doctor",
	"actor" : "David Tennant",
	"companions" : ["Rose", "Donna", "Martha"]

Could be accessed in Perl:

print $data->{name};
print ": ";
print $data->{actor};
print "\n";

Resulting in:

Tenth Doctor: David Tennant

As with arrays, the $data variable is a reference to a hash, so we use $data->{name}, NOT $data{name} to access the data.

Writing a JSON File

You use the encode() method from JSON::PP to convert a Perl data structure into JSON format. For example:

my $json = JSON::PP->new;
print $json->encode($data);

Would result in the following:

{"companions":["Rose","Donna","Martha"],"name":"Tenth Doctor","actor":"David Tennant"}

This output is correct, according to the JSON spec, however it may be desirable to make the data easier for humans to read. Adding the 'pretty' method accomplishes this:

my $json = JSON::PP->new->pretty;
print $json->encode($data);

Resulting in:

   "companions" : [
   "name" : "Tenth Doctor",
   "actor" : "David Tennant"

That is all there is to it! Opening a file and writing the JSON formatted data should be straightforward at this point.