title: How to JSON encode a hash?
body: <p>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.</p>
  
  <p><strong>My question is:</strong></p>
  
  <p>When I am in my <code>foreach</code>-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?</p>
  
  <p>Here is how I do that</p>
  
  <pre><code>use JSON;
  my $json = JSON-&gt;new;
  $json = $json-&gt;utf8;
  
  ...
  
  # use numeric sort
  foreach my $key (sort {$a &lt;=&gt; $b} (keys %act)) {
  
    # somehow insert $key and contents of $act{$key} into JSON here
  
  }
  
  # my $json_string;
  # my $data = $json-&gt;encode(%h);
  # $json_string = to_json($data);
  
  # # return JSON string
  # print $cgi-&gt;header(-type =&gt; "application/json", -charset =&gt; "utf-8");
  # print $json_string;
  </code></pre>
  
  <p><code>print Dumper \%act</code> looks like this</p>
  
  <pre><code>$VAR1 = {
            '127' =&gt; {
                       'owners' =&gt; [
                                     'm'
                                   ],
                       'users' =&gt; [
                                    'hh',
                                    'do'
                                  ],
                       'date_end' =&gt; '24/05-2011',
                       'title' =&gt; 'dfg',
                       'date_begin' =&gt; '24/05-2011',
                       'members_groups' =&gt; [],
                       'type' =&gt; 'individuel'
                     },
            '276' =&gt; {
                       ...
  </code></pre>