[sf-perl] REST data parsing

Joe Brenner doom at kzsu.stanford.edu
Tue Sep 7 18:10:06 PDT 2010


David Alban <extasia at extasia.org> wrote:

> i know little about REST.  i've been told that this example string is
> rest data, something returned by some RESTful process:
>
> {"FraudCheckMonitorResults":"TrinityCheck":1,"AccertifyCheck":1,"NumRetryListings":1,"NumRetryOrders":1,"NumPurchasedStuckOrders":1,"NumAcceptedListings":1,"NumAcceptedOrders":1,"NumRejectedListings":1,"NumRejectedOrders":1,"NumReviewedListings":1,"NumReviewedOrders":1}}
>
> a local java developer asked me (the local perl guy) how to get this
> string into a perl data structure.  when i asked, he confirmed he
> wanted something like this:
>
> FraudCheckMonitorResults => {
>   TrinityCheck            => 1,
>   AccertifyCheck          => 1,
>   NumRetryListings        => 1,
>   NumRetryOrders          => 1,
>   NumPurchasedStuckOrders => 1,
>   NumAcceptedListings     => 1,
>   NumAcceptedOrders       => 1,
>   NumRejectedListings     => 1,
>   NumRejectedOrders       => 1,
>   NumReviewedListings     => 1,
>   NumReviewedOrders       => 1,
> };


There's something a little funny about the data you posted...
The "FraudCheckMonitorResults": at the beginning and the extra "}"
at the end needs to go away for it to parse as JSON:

use JSON;

my $raw_data_munged = q{
{"TrinityCheck":1,"AccertifyCheck":1,"NumRetryListings":1,"NumRetryOrders":1,"NumPurchasedStuckOrders":1,"NumAcceptedListings":1,"NumAcceptedOrders":1,"NumRejectedListings":1,"NumRejectedOrders":1,"NumReviewedListings":1,"NumReviewedOrders":1}
};

my $perl_data = decode_json( $raw_data_munged );
print Dumper( $perl_data ), "\n";

Outputs

$VAR1 = {
          'AccertifyCheck' => 1,
          'NumReviewedListings' => 1,
          'NumAcceptedListings' => 1,
          'NumRetryListings' => 1,
          'NumRetryOrders' => 1,
          'TrinityCheck' => 1,
          'NumPurchasedStuckOrders' => 1,
          'NumReviewedOrders' => 1,
          'NumAcceptedOrders' => 1,
          'NumRejectedListings' => 1,
          'NumRejectedOrders' => 1
        };


More information about the SanFrancisco-pm mailing list