Converting array of numbers to json in perl, getting array of strings

json, perl

Solution

From the `JSON` documentation:

You can force the type to be a number by numifying it:

   my $x = "3"; # some variable containing a string
   $x += 0;     # numify it, ensuring it will be dumped as a number
   $x *= 1;     # same thing, the choice is yours.

I would try something like this:

    $json = to_json({numbers_array => [map { $_ + 0 } @numbers_array]});
    print $json;

Problem

I'm trying to convert an array of numbers to json: ``` $json = to_json("numbers_array" => \@numbers_array); print $json; ``` but the output I'm getting is: ``` {"numbers_array" => ["1","2","3"] } ``` I'm building up the array by adding elements using push(). How do I convert to json and retain the numeric type, so that the output is: ``` {"numbers_array" => [1,2,3]} ``` Thanks!

Original source

Related problems