Which Perl module would you recommend for JSON manipulation? Which Perl module would you recommend for JSON manipulation? json json

Which Perl module would you recommend for JSON manipulation?


JSON module works like a champ, but if you need a faster parser, use this one: JSON::XS, which requires a native compilation.

Note that JSON version 2.0 and above is merely a front end for JSON::XS (if installed) or JSON::PP (fallback).


[Update: I now recommend Cpanel::JSON::XS over JSON::XS. Cpanel::JSON::XS is a better maintained version of JSON::XS.]

I always use JSON::XS. Complete, robust, proven, fast*, easy to use, and even a bit of flexibility if you need it.

It's probably the most used JSON parser, though most access it through JSON (but doing so risks using slower JSON::PP instead).

* — "An order of magnitude" faster than JSON::Tiny, according to JSON::Tiny's docs.


Since you say that you are getting the data from an online source, you might consider the Mojolicious tool suite. In that way you can get the data, parse it and maybe even use JSON pointers to extract info.

Basic:

#!/usr/bin/env perluse strict;use warnings;use Mojo::UserAgent;my $ua = Mojo::UserAgent->new;my $data = $ua->get('http://openlibrary.org/search.json?title=perl%20modules')              ->res              ->json;

With url constructor and JSON pointer:

#!/usr/bin/env perluse strict;use warnings;use v5.10;use Mojo::URL;use Mojo::UserAgent;my $ua = Mojo::UserAgent->new;my $url = Mojo::URL->new('http://openlibrary.org/search.json')                   ->query( title => 'perl modules' );say $ua->get($url)       ->res       ->json('/docs/0/title_suggest');

Note that the json method on the response object either returns the whole parsed data structure or can take a pointer string (as in the second example) to return just a subset to get you going quickly. Enjoy.