Hi I´m trying to parse the results of a report setup as public but are unable to get the data. This is the script I'm working on #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request; my $ua = new LWP::UserAgent; $ua->agent("Perl API Client/1.0"); my $url = "https://myurl/report?id=53"; my $request = HTTP::Request->new("GET" => $url); my $response = $ua->request($request); print "Response::: " . $response . "\n"; use JSON; my $json_obj = JSON->new->utf8->decode($response->content); print "JSON OBJ: " . $json_obj . "\n"; print $json_obj->{rowCount} . " rows:n"; foreach my $row(@{$json_obj->{rows}}){ print $row->{Key} . ":" . $row->{Value} . "n"; } My debug print gives me: Response::: HTTP::Response=HASH(0x564ec7e6cfa8) JSON OBJ: ARRAY(0x564ec76dd7f8) Not a HASH reference at reporte53-2.pl line 23. Each time I run the script I get different 0X values. If I call the report through a web browser I get [["aa",339],["ab",99094],["am",47957],["as",1641],["gm",1],["jm",1]] And if I add &annotated=1 to my report link I get: [{"Registros":339,"Type":"aa"},{"Type":"ab","Registros":99094},{"Registros":47957,"Type":"am"},{"Type":"as","Registros":1641},{"Registros":1,"Type":"gm"},{"Registros":1,"Type":"jm"}] What am I missing for parsing the report? Regards, Alvaro |----------------------------------------------------------------------------------------| Stay safe / Cuídate/ Reste sécurisé *7* Switch off as you go / Apaga lo que no usas / Débranchez au fur et à mesure. *q *Recycle always / Recicla siempre / Recyclez toujours P Print only if absolutely necessary / Imprime solo si es necesario / Imprimez seulement si nécessaire
Hi Alvaro, The JSON data you are seeing in the browser is an array (of arrays) not a JSON object. Accordingly, $json_obj is a reference to an Perl array not a Perl hash. Hence the output JSON OBJ: ARRAY(0x564ec76dd7f8) and Perl's complaint about $json_obj being treated as hash reference: Not a HASH reference at reporte53-2.pl line 23. The same goes for $row, too. It is also an array reference. Try to change the end of your script to this: print "JSON OBJ: " . $json_obj . "\n"; print scalar(@{$json_obj}), " rows:\n"; foreach my $row (@{$json_obj}){ print join(" ", @{$row}), "\n"; } Hope this helps, Alexander
Hi Alexander, That works !! Thanks Alvaro |----------------------------------------------------------------------------------------| Stay safe / Cuídate/ Reste sécurisé *7* Switch off as you go / Apaga lo que no usas / Débranchez au fur et à mesure. *q *Recycle always / Recicla siempre / Recyclez toujours P Print only if absolutely necessary / Imprime solo si es necesario / Imprimez seulement si nécessaire Le mer. 8 juil. 2020 à 01:53, Alexander Borkowski < Alexander.Borkowski@kau.se> a écrit :
Hi Alvaro,
The JSON data you are seeing in the browser is an array (of arrays) not a JSON object. Accordingly, $json_obj is a reference to an Perl array not a Perl hash. Hence the output
JSON OBJ: ARRAY(0x564ec76dd7f8)
and Perl's complaint about $json_obj being treated as hash reference:
Not a HASH reference at reporte53-2.pl line 23.
The same goes for $row, too. It is also an array reference. Try to change the end of your script to this:
print "JSON OBJ: " . $json_obj . "\n"; print scalar(@{$json_obj}), " rows:\n";
foreach my $row (@{$json_obj}){ print join(" ", @{$row}), "\n"; }
Hope this helps,
Alexander
_______________________________________________
Koha mailing list http://koha-community.org Koha@lists.katipo.co.nz Unsubscribe: https://lists.katipo.co.nz/mailman/listinfo/koha
participants (2)
-
Alexander Borkowski -
Alvaro Cornejo