Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to JSON encode a hash?

Tags:

json

hash

perl

I would like to iterate over a hash on the server-side, and send it over to the client in the sorted order using JSON.

My question is:

When I am in my foreach-loop and have the key and complex value (see how my hash looks like at the bottom), how do I insert it in to the JSON string?

Here is how I do that

use JSON;
my $json = JSON->new;
$json = $json->utf8;

...

# use numeric sort
foreach my $key (sort {$a <=> $b} (keys %act)) {

  # somehow insert $key and contents of $act{$key} into JSON here

}

# my $json_string;
# my $data = $json->encode(%h);
# $json_string = to_json($data);

# # return JSON string
# print $cgi->header(-type => "application/json", -charset => "utf-8");
# print $json_string;

print Dumper \%act looks like this

$VAR1 = {
          '127' => {
                     'owners' => [
                                   'm'
                                 ],
                     'users' => [
                                  'hh',
                                  'do'
                                ],
                     'date_end' => '24/05-2011',
                     'title' => 'dfg',
                     'date_begin' => '24/05-2011',
                     'members_groups' => [],
                     'type' => 'individuel'
                   },
          '276' => {
                     ...
like image 679
Sandra Schlichting Avatar asked Jun 06 '11 12:06

Sandra Schlichting


People also ask

How do I encode a string in JSON?

In jQuery you can use the following code to encode the json: var data = {"key1":"value1", "key2":"value2","key3":"value3",...}; $. each(data,function(index,value)){ alert(index+" | "+value); });

What is a hash in JSON?

Referring to JSON dictionaries as hash tables would be technically incorrect, however, as there is no particular data structure implementation associated with the JSON data itself. A hash is a random looking number which is generated from a piece of data and always the same for the same input.

How is JSON encoded?

JSON is based on two basic structures: Object: This is defined as a collection of key/value pairs (i.e. key:value ). Each object begins with a left curly bracket { and ends with a right curly bracket } . Multiple key/value pairs are separated by a comma , .

What is JSON decode and encode?

Definition and Usage The json_decode() function is used to decode or convert a JSON object to a PHP object.


2 Answers

And the JSON builtin sort does not enough?

see: http://metacpan.org/pod/JSON#sort_by

Sorting is supported only with JSON:PP (Perl, not XS - AFAIK)

so:

use JSON::PP;
use warnings;
use strict;

my $data = {
        'aaa' => {
                a => 1,
                b => 2,
        },
        'bbb' => {
                x => 3,
        },
        'a2' => {
                z => 4,
        }
};

my $json = JSON::PP->new->allow_nonref;
#my $js = $json->encode($data); #without sort
my $js = $json->sort_by(sub { $JSON::PP::a cmp $JSON::PP::b })->encode($data);
print "$js\n";
like image 134
jm666 Avatar answered Oct 22 '22 12:10

jm666


Oldish post but anyone looking for sorting json output..

#!/bin/perl
use warnings;
use strict;
use Sort::Naturally;
use JSON;

my $data = {
    'a10' => {
            b => 1,
            a => 2,
    },
    'bbb' => {
            x => 3,
    },
    'a2' => {
            z => 4,
    }
};
my $json = new JSON;
$json->sort_by(sub { ncmp($JSON::PP::a, $JSON::PP::b) });
my $json_text = $json->pretty->encode ($data);
print $json_text;

{
   "a2" : {
      "z" : 4
   },
   "a10" : {
      "a" : 2,
      "b" : 1
   },
   "bbb" : {
      "x" : 3
   }
}
like image 34
Jason Avatar answered Oct 22 '22 10:10

Jason