Having trouble parsing JSON structure with JSON package

Discussion in 'Perl Misc' started by David Karr, Jun 17, 2013.

  1. David Karr

    David Karr Guest

    I have a web service that returns XML or JSON, and I'm trying to write somecode to verify the JSON form of the response. The XML verification is already working.

    The JSON looks somewhat like this (some long lists of properties replaced by "..."):

    {"sl.serviceCallResults":{"@latestTime":"1371493703000","sl.services":{"sl.service":[{"@last15SuccessRate":"0.8552632",...},{"@last15SuccessRate":"0.875",...},{"@last15SuccessRate":"0.8116788",...},{"@last15SuccessRate":"0.82133335",...},{"@last15SuccessRate":"0.81842816",...},{"@last15SuccessRate":"0.7983539",...},{"@last15SuccessRate":"0.84782606",...},{"@last15SuccessRate":"0.0",...}]},"sl.workflows":{"sl.workflow":[{"@latestTime":"1371493703000",...}]},"sl.failureExpressions":{"sl.failureExpression":["Data Error","stuff"]}}}]

    At this point, I'm simply trying to count the number of entries in the "sl.service" array.

    This is what I've hacked together so far:

    sub countJSONServices($) {
    my ($jsonText) = @_;
    my $json = JSON->new->allow_nonref;
    my $scalar = from_json($jsonText);
    printhash($scalar);
    my $serviceCallResults = $scalar->{"sl.serviceCallResults"};
    #print "serviceCallResults[" . $serviceCallResults . "]\n";
    printhash($serviceCallResults);
    my $services = $serviceCallResults->{"sl.services"};
    printhash($services);
    my $servicesList = $services->{"sl.service"};
    print "servicesList[$servicesList]\n";
    for my $service (@servicesList) {
    print "service[$service]\n";
    }
    my $servicesCount = scalar @servicesList;
    print "servicesCount[$servicesCount]\n";
    return $servicesCount;
    }

    Where "printhash()" simply is this:

    sub printhash($) {
    my ($hash) = @_;
    while ( my ($key, $value) = each(%{$hash}) ) {
    print "$key => $value\n";
    }
    }

    The output I have at this point is this:
    --------------
    sl.serviceCallResults => HASH(0x20d49990)
    @latestTime => 1371496045000
    sl.workflows => HASH(0x20a0d2d8)
    sl.services => HASH(0x20a389d8)
    sl.failureExpressions => HASH(0x20a0d260)
    sl.service => ARRAY(0x20a2bb48)
    servicesList[ARRAY(0x20a2bb48)]
    servicesCount[0]
    --------------

    Where I'm confused is that "$servicesList" should be the array of elements in the "sl.service" array, but it appears to have zero elements, although it has 5 elements in the JSON output.
     
    David Karr, Jun 17, 2013
    #1
    1. Advertising

  2. David Karr

    David Karr Guest

    On Monday, June 17, 2013 1:12:39 PM UTC-7, Ben Morrow wrote:
    > Quoth David Karr <>:
    >
    > >

    >
    > > sub countJSONServices($) {

    >
    > > my ($jsonText) = @_;

    >
    > > my $json = JSON->new->allow_nonref;

    >
    >
    >
    > This line does nothing useful. Why is it there?


    Obsolete artifact. Removed now.

    > > my $scalar = from_json($jsonText);

    >
    > > printhash($scalar);

    >
    > > my $serviceCallResults = $scalar->{"sl.serviceCallResults"};

    >
    > > #print "serviceCallResults[" . $serviceCallResults . "]\n";

    >
    > > printhash($serviceCallResults);

    >
    > > my $services = $serviceCallResults->{"sl.services"};

    >
    > > printhash($services);

    >
    > > my $servicesList = $services->{"sl.service"};

    >
    > > print "servicesList[$servicesList]\n";

    >
    > > for my $service (@servicesList) {

    >
    >
    >
    > You're not using 'strict'. Bad programmer, no cookie!
    >
    >
    >
    > $servicesList and @servicesList are completely different variables. You
    >
    > want to deref the arrayref in $servicesList, that is, @$servicesList.
    >
    >
    >
    > (You might want to consider using less verbose variable names. That much
    >
    > repetition makes the code extremely hard to read.)
    >
    >
    >
    > > print "service[$service]\n";

    >
    > > }

    >
    > > my $servicesCount = scalar @servicesList;

    >
    > > print "servicesCount[$servicesCount]\n";

    >
    > > return $servicesCount;

    >
    > > }

    >
    > >

    >
    > > Where "printhash()" simply is this:

    >
    > >

    >
    > > sub printhash($) {

    >
    > > my ($hash) = @_;

    >
    > > while ( my ($key, $value) = each(%{$hash}) ) {

    >
    > > print "$key => $value\n";

    >
    > > }

    >
    > > }

    >
    >
    >
    > You might want to consider using Data::Dumper or Data::Dump instead.
    >
    >
    >
    > Ben


    Got it. Cleaned up now, and it's working. Thanks.
     
    David Karr, Jun 17, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    245
    Florian Frank
    Jun 30, 2009
  2. having problems parsing JSON

    , Apr 22, 2006, in forum: Javascript
    Replies:
    2
    Views:
    111
  3. sajuptpm
    Replies:
    2
    Views:
    349
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    262
    dieter
    Feb 15, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    269
    Bryan Britten
    May 28, 2013
Loading...

Share This Page