Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Can not use JSON module

by karlgoethebier (Abbot)
on Jun 03, 2017 at 09:13 UTC ( [id://1192049]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Can not use JSON module
in thread Can not use JSON module

"...I am searching for function..."

Yes, sure. I got it. Here it is (as i already mentioned):

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper qw(Dumper); my $FROM_JSON = qr{ (?&VALUE) (?{ $_ = $^R->[1] }) (?(DEFINE) (?<OBJECT> (?{ [$^R, {}] }) \{ (?: (?&KV) # [[$^R, {}], $k, $v] (?{ # warn Dumper { obj1 => $^R }; [$^R->[0][0], {$^R->[1] => $^R->[2]}] }) (?: , (?&KV) # [[$^R, {...}], $k, $v] (?{ # warn Dumper { obj2 => $^R }; [$^R->[0][0], {%{$^R->[0][1]}, $^R->[1] => $^R->[2]}] }) )* )? \} ) (?<KV> (?&STRING) # [$^R, "string"] : (?&VALUE) # [[$^R, "string"], $value] (?{ # warn Dumper { kv => $^R }; [$^R->[0][0], $^R->[0][1], $^R->[1]] }) ) (?<ARRAY> (?{ [$^R, []] }) \[ (?: (?&VALUE) (?{ [$^R->[0][0], [$^R->[1]]] }) (?: , (?&VALUE) (?{ # warn Dumper { atwo => $^R }; [$^R->[0][0], [@{$^R->[0][1]}, $^R->[1]]] }) )* )? \] ) (?<VALUE> \s* ( (?&STRING) | (?&NUMBER) | (?&OBJECT) | (?&ARRAY) | true (?{ [$^R, 1] }) | false (?{ [$^R, 0] }) | null (?{ [$^R, undef] }) ) \s* ) (?<STRING> ( " (?: [^\\"]+ | \\ ["\\/&#660;bfnrt] # | # \\ u [0-9a-fA-f]{4} )* " ) (?{ [$^R, eval $^N] }) ) (?<NUMBER> ( -? (?: 0 | [1-9]\d* ) (?: \. \d+ )? (?: [eE] [-+]? \d+ )? ) (?{ [$^R, eval $^N] }) ) ) }xms; sub from_json { local $_ = shift; local $^R; eval { m{\A$FROM_JSON\z}; } and return $_; die $@ if $@; return 'no match'; } my $json = q({"k1":"v1","k2":"v2"}); print Dumper from_json($json); __END__ karls-mac-mini:Desktop karl$ ./godfather.pl $VAR1 = { 'k2' => 'v2', 'k1' => 'v1' };

Update: Oops!. Better with Data::Dumper

Thank merlyn for the regex.

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Furthermore I consider that Donald Trump must be impeached as soon as possible

Replies are listed 'Best First'.
Re^4: Can not use JSON module
by tejD (Novice) on Jun 03, 2017 at 19:54 UTC
    Hi, Thanks for function. Any alternative of Data::Dumper in the above code. As I do not have Data::Dumper module and can not install it.

      Try...

      my $hash_ref = from_json($json); while ( my ( $k, $v ) = each %$hash_ref ) { print qq($k => $v\n) }

      ...which doesn't help if things get more complex. But this doesn't matter. Just use the result - it's a Perl data structure ;-)

      See also Re: How can I visualize my complex data structure?

      «The Crux of the Biscuit is the Apostrophe»

      Furthermore I consider that Donald Trump must be impeached as soon as possible

        Hi, Thank you everyone for helping me. Thanks karlgoethebier again for providing the function, it solved my problem. Although, I tried all JSON modules suggested in the discussion and they are working fine on ubuntu but not on the board as I am unable to install modules on the board. Problem resolved. one query as below : I have pasted the function in one of the cgi file and tested it.It is working fine and I am able to extract json. As I have multiple cgi files, do I need to copy-paste that function in each file or is there any way to keep that function in separate file and use it, if possible how? Thanks

        Hi,

        Thanks.

        I tried the given original code by you(keeping the Data dumper). I have changed the "shebang line" and then executed it. I got following error as below

        Use of uninitialized value in regexp compilation at jjson.pl line 93. Use of uninitialized value in regexp compilation at jjson.pl line 93. Eval-group not allowed at runtime, use re 'eval' in regex m/ (?&VALUE) (?{ $_ = $^R->[1] }) (?(DEFINE) (?<OBJECT> (?{ [$^R, {}] }) { (?: (?&KV) # [[$^R, {}], $k, $v] .../ at jjson.pl line 93.

        I executed as ./jjson.pl. Also as perl jjson.pl. Both from command prompt.

        I tried to search error, But did not find anything helpful.

        Please help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192049]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found