Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Having trouble parsing JSON structure with JSON package

6 views
Skip to first unread message

David Karr

unread,
Jun 17, 2013, 3:15:39 PM6/17/13
to
I have a web service that returns XML or JSON, and I'm trying to write some code 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.

Ben Morrow

unread,
Jun 17, 2013, 4:12:39 PM6/17/13
to

Quoth David Karr <davidmic...@gmail.com>:
>
> sub countJSONServices($) {
> my ($jsonText) = @_;
> my $json = JSON->new->allow_nonref;

This line does nothing useful. Why is it there?

> 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

David Karr

unread,
Jun 17, 2013, 4:56:19 PM6/17/13
to
On Monday, June 17, 2013 1:12:39 PM UTC-7, Ben Morrow wrote:
> Quoth David Karr <davidmic...@gmail.com>:
>
> >
>
> > sub countJSONServices($) {
>
> > my ($jsonText) = @_;
>
> > my $json = JSON->new->allow_nonref;
>
>
>
> This line does nothing useful. Why is it there?

Obsolete artifact. Removed now.
Got it. Cleaned up now, and it's working. Thanks.
0 new messages