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

Hi,

I'm passing this JS object to my script:

[{feedId:6161,feedType:'forum'},{feedId:1469,feedType:'category'}]

I then need to convert it to the following format in Perl:

my @feedData = ({ 'feedId' => 6161, 'feedType' => 'forum' },{ 'feedId' => 1469, 'feedType' => 'category' });

Could someone kindly give me some pointers??

Thank you!

Replies are listed 'Best First'.
Re: Convert JS array of objects into Perl array of Hashes
by Fletch (Bishop) on Dec 08, 2009 at 17:56 UTC
Re: Convert JS array of objects into Perl array of Hashes
by codeacrobat (Chaplain) on Dec 08, 2009 at 21:38 UTC

    This is not exactly valid JSON for JSON::XS. Quotes are either missing or single quotes.

    A bit of toying reveals:

    echo "[{feedId:6161,feedType:'forum'},{feedId:1469,feedType:'category' +}]" | perl -MData::Dump=dd -MJSON::XS -ne 's{(\w+):}{"$1":}g; s{@{[ch +r 39]}}{"}g;dd decode_json($_)' [ { feedId => 6161, feedType => "forum" }, { feedId => 1469, feedType => "category" }, ]
    Anyone know how to make JSON::XS more tolerant?

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
      Anyone know how to make JSON::XS more tolerant?

      JSON::XS has a "relaxed" mode (F_RELAXED in the XS code). Currently, it allows:

      • lists with a final comma (as in Perl)
      • Shell-style comments (from # to end of line) where JSON allowes white space

      This looks like a proper way to allow unquoted keys and the like, you "just" have to modify the XS code.

      The one thing that really drives me mad is the shell-style comment. Why on earth would someone want shell comments in JSON? JSON is derived from Javascript, and it is designed to be evaluateable by current browsers. Javascript has inherited its comment syntax from C and C++, i.e. // for single line comments and /* */ for comment blocks. I would like to see those comments supported by JSON::XS. No browser understands shell comments, so they should be removed.

      A different approach could be a subclass that uses the incremental parsing feature.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Convert JS array of objects into Perl array of Hashes
by AnomalousMonk (Archbishop) on Dec 08, 2009 at 23:17 UTC

    I don't know about JSON, but an alternative (and possibly fragile) approach using a regex and eval would be:

    >perl -wMstrict -le "my $js = q{[{feedId:6161,feedType:'forum'},{feedId:1469,feedType:'cat +egory'}]}; $js =~ s{ : }{ => }xmsg; print $js; my @array = eval qq{ \@{ $js } }; use Data::Dumper; print Dumper \@array; " [{feedId => 6161,feedType => 'forum'},{feedId => 1469,feedType => 'cat +egory'}] $VAR1 = [ { 'feedId' => 6161, 'feedType' => 'forum' }, { 'feedId' => 1469, 'feedType' => 'category' } ];

    Note that you had better trust your input data pretty well if you take an approach like this.