Which Perl module would you recommend for JSON manipulation?

10,236

Solution 1

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).

Solution 2

[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.

Solution 3

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.

Solution 4

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!

Solution 5

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.

Share:
10,236
smonff
Author by

smonff

Interested in all kind of web development tools, especially if it involve Mojolicious. Can talk and read Modern Perl and JavaScript. Also like to play with data, either plain text or databases. The GNU/Linux toolkit offers all what I need. Very enthusiastic about learning new things, gardening, playing the drums and feeding my teenage cat.

Updated on June 17, 2022

Comments

  • smonff
    smonff almost 2 years

    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?