kanewilliam7777 has asked for the wisdom of the Perl Monks concerning the following question:

In PERL using CGI concept have to get the JSON POST values

$cgi = CGI->new(); my $json = new JSON; my $params =$cgi->param("POSTDATA"); print Dumper($params); $VAR1 = '$VAR1 = \'{ "name" : "dsfsdfs", "age" : "23", "city" : "vvvvv", "state" : "aaaa", }\';

How to get above $params value in array format.

Replies are listed 'Best First'.
Re: Convert JSON to Array in CGI
by hippo (Archbishop) on Sep 10, 2018 at 14:36 UTC

    Update: On reflection, you are actually POSTing JSON. It's the use of Dumper which confused me - why not just print the scalar?

    use JSON; my $json = '{ "name" : "dsfsdfs", "age" : "23", "city" : "vvvvv", "state" : "aaaa" }'; my $hashref = decode_json $json; my @array = %$hashref; print join ', ', @array;

    You haven't said what you expect the "array format" to look like. Therefore I'll just guess that you want the keys and the values.

    my @array = %$params;

    That should be all you need. See perldoc perlref for info on references. BTW, what you've asked has nothing to do with JSON whatsoever.

Re: Convert JSON to Array in CGI
by haukex (Archbishop) on Sep 10, 2018 at 16:01 UTC
    $VAR1 = '$VAR1 = \'{ "name" : "dsfsdfs", "age" : "23", "city" : "vvvvv", "state" : "aaaa", }\';

    Wait, is that really the output that you get from print Dumper($params);? That would mean that $params is in fact a string of Data::Dumper output - in other words someone is POSTing that to your script? Could you please clarify, preferably with an SSCCE?

Re: Convert JSON to Array in CGI
by marto (Cardinal) on Sep 10, 2018 at 14:33 UTC

      I have downvoted your post. Although I understand your concerns about CGI, your warnings are IMO becoming spam interjected into every post that contains the term CGI.pm

        The advice was for OP, not you, you are already aware of the situation. Based on OPs interactions so far, there's a very good chance they're not aware of the issue.

Re: Convert JSON to Array in CGI
by poj (Abbot) on Sep 10, 2018 at 14:33 UTC
    How to get above $params value in array format.

    Do you mean

    my @array = ("dsfsdfs", "23", "vvvvv", "aaaa");

    or

    my @array = ("name","dsfsdfs","age","23","city","vvvvv","state","aaaa");
    poj

      I need this type

      my @array = ("name","dsfsdfs","age","23","city","vvvvv","state","aaaa");

Re: Convert JSON to Array in CGI
by ikegami (Patriarch) on Sep 10, 2018 at 22:33 UTC

    $params doesn't contain JSON as you claim; it contains Perl code. There's no safe way to handle that.

      Of course there are very safe ways to handle a delimited subset of Perl code such as Data::Dumper produces. There just is no safe/sane way to handle arbitrary Perl code.

      As long as you restrict yourself to eliminating the $VAR1 =, the rest is almost JSON, and JSON can be safely parsed. After some searching merlyn wrote such a Parser for Data::Dumper and posted it online.

        I was aware that it was theoretically possible to verify that the output was part of that subset, but I wasn't aware of merlyn's work. Thanks!

        (Of course, P::RD is insanely slow, so this could use some work.)