Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Build JSON With Perl

by srchulo (Sexton)
on Nov 11, 2011 at 06:25 UTC ( #937530=perlquestion: print w/ replies, xml ) Need Help??
srchulo has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am trying to build JSON such as this with Perl:

[{"name":"picture1.jpg","size":902604,"url":"\/\/example.org\/files\/picture1.jpg","thumbnail_url":"\/\/example.org\/thumbnails\/picture1.jpg","delete_url":"\/\/example.org\/upload-handler?file=picture1.jpg","delete_type":"DELETE"},{"name":"picture2.jpg","size":841946,"url":"\/\/example.org\/files\/picture2.jpg","thumbnail_url":"\/\/example.org\/thumbnails\/picture2.jpg","delete_url":"\/\/example.org\/upload-handler?file=picture2.jpg","delete_type":"DELETE"}]

Essentially it is JSON array of JSON objects. However, I am not finding any good tutorials online. Does anyone have any suggestions? Currently I am using the JSON::XS module to decode, but I'm open to anything as long as it works.

Currently I have this code:

my %hash = {}; $hash{'lool'} = "WEEEE"; $hash{'YAY'} = "WEEEE"; my $json = JSON->new->allow_nonref; my $json_text = $json->encode(\%hash);

Only problem is that it creates this:

{"lool":"WEEEE","YAY":"WEEEE","HASH(0x8b8410)":null}

Which also include the hash reference itself in it. Any help would be appreciated!

Many Thanks,
srchulo

Comment on Build JSON With Perl
Select or Download Code
Re: Build JSON With Perl
by MVS (Monk) on Nov 11, 2011 at 07:16 UTC

    Actually, that's not the hash reference itself that you're seeing. It's the third element of the hash, which was created by the empty curly braces. Change:

    my %hash = {};

    to:

    my %hash;

    and it should work the way you're looking for.

      Using strict and warnings would have shown that:

      Reference found where even-sized list expected at -e line 1.

      Good catch though, I missed it in converting the example to a one-liner


      Enjoy, Have FUN! H.Merijn
Re: Build JSON With Perl
by Tux (Monsignor) on Nov 11, 2011 at 07:17 UTC

    Using JSON-2.53 I get:

    $ perl -MJSON -E'say JSON->new->allow_nonref->encode({lool=>"WEEEE",YA +Y=>"WEEEE"})' {"lool":"WEEEE","YAY":"WEEEE"}

    Did you miss something in the example code?


    Enjoy, Have FUN! H.Merijn
Reaped: Re: Build JSON With Perl
by NodeReaper (Curate) on Nov 14, 2011 at 12:53 UTC
Reaped: Re: Build JSON With Perl
by NodeReaper (Curate) on Nov 15, 2011 at 13:33 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://937530]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2014-09-20 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls