Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Having trouble parsing JSON structure with JSON package

Reply
Thread Tools

Having trouble parsing JSON structure with JSON package

 
 
David Karr
Guest
Posts: n/a
 
      06-17-2013
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":"137149370 3000","sl.services":{"sl.service":[{"@last15SuccessRate":"0.8552632",...},{"@last15Su ccessRate":"0.875",...},{"@last15SuccessRate":"0.8 116788",...},{"@last15SuccessRate":"0.82133335",.. .},{"@last15SuccessRate":"0.81842816",...},{"@last 15SuccessRate":"0.7983539",...},{"@last15SuccessRa te":"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(0x20a0d2d
sl.services => HASH(0x20a389d
sl.failureExpressions => HASH(0x20a0d260)
sl.service => ARRAY(0x20a2bb4
servicesList[ARRAY(0x20a2bb4]
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.
 
Reply With Quote
 
 
 
 
David Karr
Guest
Posts: n/a
 
      06-17-2013
On Monday, June 17, 2013 1:12:39 PM UTC-7, Ben Morrow wrote:
> Quoth David Karr <(E-Mail Removed)>:
>
> >

>
> > 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:umper or Data:ump instead.
>
>
>
> Ben


Got it. Cleaned up now, and it's working. Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM
having problems parsing JSON paulfe4@gmail.com Javascript 2 04-22-2006 09:51 PM



Advertisments