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

Hi, Sorry for the given JSON(it was only for example). I also checked and tried to use JSON::PP which comes as default. But my current perl does not have this also. I have installed perl 5.22.0-r0 via OPKG. It shows very basic module. I am not doing this on our general linux(eg ubuntu). I am doing it on board which has linux OS. So, I used OPKG to install perl which is supported by linux on that board. So, I am searching for function or something that I can use in my CGI script to decode json. Thanks.

Replies are listed 'Best First'.
Re^3: Can not use JSON module
by Corion (Patriarch) on Jun 03, 2017 at 09:09 UTC

    You can install JSON::PP by copying the files onto your machine.

    Also, consider maybe JSON::Tiny, which can also be installed by copying.

Re^3: Can not use JSON module
by karlgoethebier (Abbot) on Jun 03, 2017 at 09:13 UTC
    "...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

      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

Re^3: Can not use JSON module
by morgon (Priest) on Jun 05, 2017 at 20:57 UTC
    If you are using opkg to install packages you are not using "it on a board which has linux OS".

    You seem to be using OpenWrt which is a different OS.

    It may help to indicate which version you are using and what device you use.