12

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?

smonff
  • 3,399
  • 3
  • 36
  • 46
  • JSON::Tiny's use case is situations where you need a JSON implementation that is lightweight and that doesn't require a compiler as XS does. It's good for bundling too. If you need a pure-Perl JSON package, it's for you. If you can afford the XS toolchain, JSON::XS is faster. – DavidO Mar 05 '14 at 22:40
  • Thanks for the precisions @DavidO, I've finally used JSON::XS and it's great and simple to use. – smonff Mar 06 '14 at 11:32
  • 2
    Cpanel::JSON::XS is fastest and most up-to-date See the Comparing the speed of JSON decoders article http://perlmaven.com/comparing-the-speed-of-json-decoders – szabgab Apr 03 '15 at 21:20
  • 1
    @szagab The URL you give actually doesn't say it's the fastest. Also, the Cpanel fork doesn't contain any fixes, only some dirty hacks that can bite you later. – Remember Monica Oct 16 '15 at 22:44

6 Answers6

18

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

ikegami
  • 367,544
  • 15
  • 269
  • 518
Miguel Prz
  • 13,718
  • 29
  • 42
12

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

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • And if you encounter one of the many bugs in JSON::XS, you might want to fallback to Cpanel::JSON::XS, which got them fixed. Or use JSON::Any or JSON::MaybeXS, which picks the best one. – rurban Apr 11 '15 at 19:57
  • Calling JSON::XS "actively maintained" might be a long shot, and you might very alone with this opinion. He refuses to use a bugtracker because "RT sucks", so you cannot see the errors other people are reporting, which I have to fix then for our fork. Most of these errors are ignored for non-technical reasons. RT sucks, yes, I agree, so I use a better tracker. But this is no excuse to ignore most error reports. – rurban Apr 12 '15 at 13:10
  • @rurban, I was thinking of Text::CSV_XS. I don't know about JSON::XS's maintenance history. The author of JSON::XS is indeed a pain to deal with. (For eaxmple, he refuses to to use CPAN-RT, but doesn't provide an alternate means of reporting in the META files, so CPAN says to use CPAN-RT...) – ikegami Apr 12 '15 at 15:38
  • JSON::XS is quite clearly actively maintained. @rurban: You should stop spreading FUD and maybe push your cpanel module a bit less aggressively, given that it is mostly dirty hacks. – Remember Monica Oct 16 '15 at 22:48
11

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.

Joel Berger
  • 20,180
  • 5
  • 49
  • 104
  • Seems very interesting and simple to use. I don't mention we were using Catalyst but as far as I know their is no equivalent for it. – smonff Jan 30 '13 at 09:31
  • 2
    Mojolicious was actually written by the author of Catalyst, originally as its replacement. Now it is commonly accepted that the two are distinct projects. Many would claim that its silly to use Mojolicious in a Catalyst project, but since it is so light, I see no reason not to personally. :-) – Joel Berger Jan 30 '13 at 12:53
8

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!

Ranguard
  • 2,756
  • 3
  • 20
  • 15
  • Nice you report it to Task::Kensho ! As there is some modules about XML, JSON seems important too. – smonff Apr 03 '15 at 18:24
  • Task::Kensho is managed on [github](https://github.com/EnlightenedPerlOrganisation/task-kensho) suggestions in the form of tickets (or better Pull Requests!) are always welcome. Everything in TK goes through a discussion and review process that's based out of `#epo-ec` on `irc.perl.org`. – perigrin Apr 03 '15 at 18:59
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.

Community
  • 1
  • 1
brian d foy
  • 129,424
  • 31
  • 207
  • 592
2

JSON::XS would be a good module.

Amey
  • 8,470
  • 9
  • 44
  • 63