Bruno Pérel
Bruno Pérel

Reputation: 587

Encoding an array of hashes in Perl

I'm trying to do something that seems to be very simple, but I can't figure out how to do it in Perl : I want to output a JSON-formatted array of hashes.

The array of hashes in question is actually an array of DBIx::MyParse Items object instances. Here is my code :

use strict;
use DBIx::MyParse;
use JSON::PP;
my $json = JSON::PP->new->ascii->pretty->allow_nonref;

our $parser = DBIx::MyParse->new( database => "test", datadir => "/tmp/myparse" );
our $query = $parser->parse("UPDATE table1 SET field1 = 1;");

$json->convert_blessed(1);
print $json->encode(@{$query} );

And this is what this script outputs :

"SQLCOM_UPDATE"

Which is actually the first element of the array that I want to output as a whole. Here is the content of the array that I see when I step-by-step debug the script : Query debug value

I would like to have the whole structure in my JSON output. How can I achieve this ?

Upvotes: 2

Views: 279

Answers (1)

mob
mob

Reputation: 118605

JSON::encode just expects a single argument, not a list. Use $json->encode( $query ).

Upvotes: 5

Related Questions