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

Just a quick question, how can I "convert" a JSON string to a hash so I can reference it as $hashname{keyname} instead of having to use $hashname->{keyname} for consistancy in my program? I use "map" to split the query string:
sub QueryData { $QD_In = $_[0]; @QD_Pair = split('&',$QD_In); %QD_Out = map{split('=',$_,2)} @QD_Pair; return (%QD_Out); }
I'm thinking there is a way using "map" to fix the JSON string, but I can't find any references to that affect on the internet. I'm going to continue searching here in hopes that someone knows an answer. Thanx again, you guys are always a bunch of help...
-Gary

I tried re-inventing the wheel again, but everytime I push it, it still falls flat on it's side...

Replies are listed 'Best First'.
Re: JSON to HASH
by BrowserUk (Patriarch) on May 15, 2016 at 04:38 UTC
    so I can reference it as $hashname{keyname} instead of having to use $hashname->{keyname}

    Leaving aside issues about the problems with processing query strings in this way ....

    How are you using the subroutine you posted?

    Because if you use it in the only way that seems sensible, it produces the result you want:

    sub QueryData { $QD_In = $_[0]; @QD_Pair = split('&',$QD_In); %QD_Out = map{split('=',$_,2)} @QD_Pair; return (%QD_Out); };; %hash = QueryData( 'fred=1&Bill=2&John=3' );; print $hash{fred};; 1 print $hash{John};; 3

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I use this particular routine asw a "perl module" for when i want to break down the querystring that I pass to another page/script, if you have another way, by all means, please offer it, it took me some time to figure out what i did get *heh*, but, my question is how i can read in a json string from my server and format it so I can reference it as $hashname{keyname} instead of the way I have "read" that I have to reference it as $hashname->{$keyname} with the "arrow" designator... I "use JSON" of course here. Is that what you were asking ? I can offer way more if neccessary, I was just looking for a way to maybe "map" the JSON string to a key/value %hash

      I tried re-inventing the wheel again, but everytime I push it, it still falls flat on it's side...

        If you do:

        my $h = from_json( to_json( { a => 1, b => 2, c => [ 1..10 ], d => { ' +a'..'z' } } ) );;

        Then to reference key 'a' you need $h->{a};

        You can (at a small performance penalty) avoid that by doing:

        my %h = %{ from_json( to_json( { a => 1, b => 2, c => [ 1..10 ], d => +{ 'a'..'z' } } ) ) };; print $h{a};; 1

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.