Having trouble parsing JSON structure with JSON package


D

David Karr

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]
 
Ad

Advertisements

D

David Karr

This line does nothing useful. Why is it there?

Obsolete artifact. Removed now.
my $scalar = from_json($jsonText);

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top