in reply to Can not use JSON module

"...alternative way to parse json without json module?"

merlyn tried it some years ago at JSON parser as a single Perl Regex. But i guess your JSON needs to be valid however.

Minor update: Fixed link.

«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^2: Can not use JSON module
by tejD (Novice) on Jun 03, 2017 at 08:46 UTC
    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.

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

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

      "...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.
      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.