1

i have a file shown like below:

{
"status": "success",
"msg": {
    "status": "success",
    "inscount": 2,
    "critical": 0,
    "result": [
        {
            "Insulin": "Insuman Rapid",
            "morning change": 0,
            "noon change": 1,
            "evening change": 0,
            "action": 3,
            "change morning from": "22",
            "change noon from": "9",
            "change evening from": "20",
            "change morning to": "22",
            "change noon to": "12",
            "change evening to": "20",
            "change type": "1"
        },
        {
            "Insulin": "Insuman basal",
            "morning change": 0,
            "noon change": 0,
            "evening change": 0,
            "action": null,
            "change morning from": "7",
            "change noon from": "6",
            "change evening from": "8",
            "change morning to": "7",
            "change noon to": "6",
            "change evening to": "8",
            "change type": “1”
        }
    ],
    "balance": "9974"
}

}

this is a JSON response from a web service and i am saving it into a temp. file. i want to extract the result array into a perl array of object.

it was generated with the following service call

system("curl -X POST -H '$CONTENT_TYPE'  -d '$ARGS'  -o  $TEMP_FILE  $SERVICE_URL 2>/dev/null");

I am using this code to extract the status, critical and inscount

    if (open(OUTFILE,"<$TEMP_FILE")) {
    while(<OUTFILE>) {
        chomp;
        if (/status\"?\:\s*\"success\"/) {
            $SUCCESS=1;
            print"Success File open********* Febin :) \n";
        }

        if (/critical\"?\:\s*\"1\"/) {
            $CRITICAL=1;
        }

        if (/change type\"?\:\s*\"(\d+)\"/) {
            $CHANGE_TYPE=$1;
        }

        if (/balance\"?\:\s*\"([^\"]+)\"/) {
            $BALANCE=$1;
        }

        foreach $key (keys %TIME_VALUES) {
            if(/$key\schange\"?\:\s*\"1\"/) {
                $TIME_VALUES{$key}[0] = 1;
            }

            if(/change $key from\"?\:\s*\"([^\"]+)\"/) {
                $TIME_VALUES{$key}[1] = $1;
            }

            if(/change $key to\"?\:\s*\"([^\"]+)\"/) {
                $TIME_VALUES{$key}[2] = $1;
            }
        }
    }
    close(OUTFILE);

is there any way to do this please help

i am new to perl scripting

2
  • 2
    Also, why the need to save to a temp file when you can use LWP + a JSON parsing module from CPAN? Commented Feb 16, 2014 at 9:14
  • @john jensen. because i am editing an existing asterisk IVR code it has been done before by someone else. With a Change in service the Json response will send result as an array before this it was not there Commented Feb 16, 2014 at 9:51

1 Answer 1

3

Maybe don't understand fully the question, but IMHO the following can work:

use Modern::Perl;
use JSON::XS;
use Encode;
use File::Slurp;
use Data::Dumper;

my $file = "./data.json";
my $data = decode_json Encode::encode 'utf8', read_file $file, { binmode => ':utf8' } ;

my $res;
$res->{$_} = $data->{msg}->{$_} for( qw(status critical inscount));

say Dumper $res;

for the your input file produces:

$VAR1 = {
          'critical' => 0,
          'inscount' => 2,
          'status' => 'success'
        };

You should to use JSON::XS for parsing JSON to internal perl structure, and can omit the encode/utf parts if your data is only ascii...

EDIT - for increased readability - commented:

use Modern::Perl;  #use some modern perl features, like: say and strict and warnings

                   #"load" some modules:
use JSON::XS;      #for the JSON parsing
use Encode;        #module for encoding from/to different encodings
use File::Slurp;   #module for reading files into a variable
use Data::Dumper;  #module for dumping data structures

my $file = "./data.json";  #the filename, where your "JSON" data is

#read the file content into the variable (read_file - provided by the File::SLurp)
my $json_from_file = read_file $file, { binmode => ':utf8' };

#encode
my $encoded_json = Encode::encode 'utf8', $json_from_file;

#convert JSON to internal perl-data structure
my $perl_data = decode_json $encoded_json;

say "=== the data structure ===";
say Dumper $perl_data;

# copy the needed data
my $status   = $perl_data->{msg}->{status};
my $critical = $perl_data->{msg}->{critical};
my $inscount = $perl_data->{msg}->{inscount};
#this is useless, because you can use the $perl_data->{msg}->{status} directly
Sign up to request clarification or add additional context in comments.

4 Comments

but i want to read this from the file
That's exactly what he's doing
@FebinFathah added explanation
this code is working for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.