Discussion:
Struggling with Perl data structure for JSON object
(for gammel til at besvare)
Thomas Elsgaard
2011-12-10 16:22:57 UTC
Permalink
Hi CPH.pm

I am struggling a little with storing a mysql result set into a JSON object.

Which requires a data structure like this:

my $data = [
{
'servers' => [
{
'DateTime' => '2011-11-13 13:04:00',
'TotalNumberOfUsersRegistered' => '1205.00'
},
{
'DateTime' => '2011-11-13 13:00:00',
'TotalNumberOfUsersRegistered' => '1205.00'
}
]
}
];


Any good suggestions for how i can get the result set from my data
fetch routine below, into a data structure like the one above, in a
"pretty way" ?


#####################

while(my $ref = $sth->fetchrow_hashref()) {
print "DateTime: $ref->{'DateTime'}\n";
print "TotalNumberOfUsersRegistered: $ref->{'TotalNumberOfUsersRegistered'}\n";
}

#####################


The end result should be a JSON object like this:

[{"TotalNumberOfUsersRegistered":"1205.00","DateTime":"2011-11-13
13:04:00"},{"TotalNumberOfUsersRegistered":"1205.00","DateTime":"2011-11-13
13:00:00"}]

Best regards

///Thomas
Niels Jakob Darger
2011-12-10 17:11:04 UTC
Permalink
I would use the excellent JSON::XS module. There is also the pure Perl
JSON::PP module (I've never used it but I would imagine it's a slower
drop-in replacement for JSON::XS). Alternatively you can use the JSON
module which loads whatever is on the system, preferring JSON::XS.

use JSON::XS;
[...]
print encode_json($data->[0]{servers});

I wouldn't generate JSON myself, you'll eventually end up generating a
document which is not well-formed if there's any character (set)
surprises etc. The JSON modules handle everything - escaping, UTF-8 etc.

Hilsen / regards,
Niels Jakob Darger
Post by Thomas Elsgaard
Hi CPH.pm
I am struggling a little with storing a mysql result set into a JSON object.
my $data = [
{
'servers' => [
{
'DateTime' => '2011-11-13 13:04:00',
'TotalNumberOfUsersRegistered' => '1205.00'
},
{
'DateTime' => '2011-11-13 13:00:00',
'TotalNumberOfUsersRegistered' => '1205.00'
}
]
}
];
Any good suggestions for how i can get the result set from my data
fetch routine below, into a data structure like the one above, in a
"pretty way" ?
#####################
while(my $ref = $sth->fetchrow_hashref()) {
print "DateTime: $ref->{'DateTime'}\n";
print "TotalNumberOfUsersRegistered: $ref->{'TotalNumberOfUsersRegistered'}\n";
}
#####################
[{"TotalNumberOfUsersRegistered":"1205.00","DateTime":"2011-11-13
13:04:00"},{"TotalNumberOfUsersRegistered":"1205.00","DateTime":"2011-11-13
13:00:00"}]
Best regards
///Thomas
Thomas Elsgaard
2011-12-10 17:24:27 UTC
Permalink
Post by Niels Jakob Darger
I would use the excellent JSON::XS module. There is also the pure Perl
JSON::PP module (I've never used it but I would imagine it's a slower
drop-in replacement for JSON::XS). Alternatively you can use the JSON module
which loads whatever is on the system, preferring JSON::XS.
use JSON::XS;
[...]
print encode_json($data->[0]{servers});
I wouldn't generate JSON myself, you'll eventually end up generating a
document which is not well-formed if there's any character (set) surprises
etc. The JSON modules handle everything - escaping, UTF-8 etc.
Hilsen / regards,
Niels Jakob Darger
Hi Jakob

Yes, i will use a dedicated JSON module for converting the data
structure into the JSON object, but my main problem is to create the
perl data structure, in order to encode it with JSON:XS:

my $data = [
{
'servers' => [
{
'DateTime' => '2011-11-13 13:04:00',
'TotalNumberOfUsersRegistered' => '1205.00'
},
{
'DateTime' => '2011-11-13 13:00:00',
'TotalNumberOfUsersRegistered' => '1205.00'
}
]
}
];

///Thomas
Niels Jakob Darger
2011-12-10 17:38:36 UTC
Permalink
Ah, I see. How about something along the lines of:

my @servers;
while (my $ref = $sth->fetchrow_hashref()) {
push @servers, $ref;
}
print encode_json(\@servers);

Or, if you need the full structure (your example suggests not):

my $data = [
{
servers => []
}
];

while (my $ref = $sth->fetchrow_hashref()) {
push @{$data->[0]{servers}}, $ref;
}

Completely untested, I don't have a Perl/DBI setup at hand...

Jakob
Post by Thomas Elsgaard
Hi Jakob
Yes, i will use a dedicated JSON module for converting the data
structure into the JSON object, but my main problem is to create the
my $data = [
{
'servers' => [
{
'DateTime' => '2011-11-13 13:04:00',
'TotalNumberOfUsersRegistered' => '1205.00'
},
{
'DateTime' => '2011-11-13 13:00:00',
'TotalNumberOfUsersRegistered' => '1205.00'
}
]
}
];
///Thomas
Thomas Elsgaard
2011-12-10 17:46:55 UTC
Permalink
Post by Niels Jakob Darger
while (my $ref = $sth->fetchrow_hashref()) {
}
my $data = [
  {
    servers => []
   }
];
while (my $ref = $sth->fetchrow_hashref()) {
}
Hi Niels

Damnn i need to read some more about Perl data structures, that was
much more simple than i thought, it works like a charm

Thanks a lot for the help!!

///Thomas
Niels Jakob Darger
2011-12-10 22:26:37 UTC
Permalink
No worries :-)

Jakob
Post by Thomas Elsgaard
Post by Niels Jakob Darger
while (my $ref = $sth->fetchrow_hashref()) {
}
my $data = [
{
servers => []
}
];
while (my $ref = $sth->fetchrow_hashref()) {
}
Hi Niels
Damnn i need to read some more about Perl data structures, that was
much more simple than i thought, it works like a charm
Thanks a lot for the help!!
///Thomas
Loading...