kulls has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,
I'm having nested array reference.
Can anyone suggest me How can I convert into JSON data.?
my $data = [ [ "264","O","3"], [ "24","ED","5","Enh"], [ "368","B", "fng"] ];

-kulls

Replies are listed 'Best First'.
Re: How to convert nested arrayref to JSON data?
by merlyn (Sage) on Dec 01, 2007 at 18:54 UTC
    use JSON qw(objToJson); # from the CPAN my $data = [ [ "264","O","3"], [ "24","ED","5","Enh"], [ "368","B", "fng"] ]; print objToJson($data), "\n";
    results in:
    [[264,"O",3],[24,"ED",5,"Enh"],[368,"B","fng"]]
    Close enough?
Re: How to convert nested arrayref to JSON data?
by Your Mother (Archbishop) on Dec 01, 2007 at 20:41 UTC

    I'll take this chance to booster for JSON:XS too. I dropped it into serialize.pl and got this-

    Modules JSON : 1.07 JSON::PC : 0.01 JSON::Syck : 0.26 JSON::XS : 1.53 Storable : 2.15 XML::Simple : 2.14 YAML : 0.65 YAML::Syck : 0.99 Deflate Rate YAML XML::Simple JSON YAML::Syck Storable JSON:: +Syck JSON::PC JSON::XS YAML 107/s -- -89% -92% -98% -98% +-98% -99% -99% XML::Simple 990/s 822% -- -24% -81% -81% +-81% -92% -93% JSON 1305/s 1115% 32% -- -75% -75% +-76% -90% -90% YAML::Syck 5207/s 4748% 426% 299% -- -1% + -2% -59% -62% Storable 5239/s 4778% 429% 301% 1% -- + -2% -59% -62% JSON::Syck 5338/s 4870% 439% 309% 3% 2% + -- -58% -61% JSON::PC 12687/s 11712% 1181% 872% 144% 142% +138% -- -7% JSON::XS 13652/s 12611% 1279% 946% 162% 161% +156% 8% -- Inflate Rate YAML XML::Simple JSON YAML::Syck JSON::Syck JSO +N::PC Storable JSON::XS YAML 82.2/s -- -62% -84% -98% -98% + -99% -100% -100% XML::Simple 217/s 165% -- -59% -94% -94% + -97% -99% -99% JSON 527/s 542% 143% -- -85% -85% + -94% -97% -97% YAML::Syck 3412/s 4052% 1470% 547% -- -6% + -59% -82% -83% JSON::Syck 3622/s 4307% 1566% 587% 6% -- + -56% -81% -81% JSON::PC 8295/s 9994% 3716% 1473% 143% 129% + -- -56% -57% Storable 18980/s 22996% 8631% 3499% 456% 424% + 129% -- -3% JSON::XS 19504/s 23634% 8872% 3598% 472% 439% + 135% 3% --

    I did find reserialization (roundtrip) problems with vanilla JSON.pm when using it for a project recently.

Re: How to convert nested arrayref to JSON data?
by holli (Abbot) on Dec 01, 2007 at 18:57 UTC
    use JSON::Any; $json = JSON::Any->new()->encode( $yourAofA );


    holli, /regexed monk/