As usual, I'm happy to deal with CPAN because it got all we need. As usual, I'm lost because there is plenty of stuff.
I can find the core JSON
one by myself, and feel enthusiastic by a JSON::Tiny
other.
My needs are very simple (parsing stuffs from the Open Library API) and, maybe someday, expose our own data.
Is there any other modules that you like for this task?
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).
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 perl
use 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 perl
use 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.
I would recommend JSON::MaybeXS - Uses Cpanel::JSON::XS with a fallback to JSON::XS and JSON::PP.
Cpanel::JSON::XS has improvements over JSON::XS, so JSON::MaybeXS makes your code nice and portable.
Usually I would look to Task::Kensho if I'm not sure of which module to use for a specific situation, though they don't have JSON at the moment, I've reported it to them!
I've started using Mojo::JSON every chance I get. The Mojolicious is easy to install and as Joel Berger showed in his answer, it comes with much more.
JSON::XS would be a good module.