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