Solved

parse json with perl

Posted on 2015-01-03
2
621 Views
Last Modified: 2015-01-05
Hi,

Im trying to parse some simple json with a perl script.

Heres the Json :
[
    {
        "to_user": "1234567",
        "message": "test 1"
    },
    {
        "to_user": "345456657",
        "message": "test 2"
    },
    {
        "to_user": "45667324",
        "message": "test 3"
    }
]

Open in new window


my perl errors with "NOT A HASH REFERENCE", I obviously have my code wrong, but cant find out how to fix it!

Heres my Code :
use HTTP::Request;
use LWP::UserAgent;




my $json = encode_json \%message;
my $uri = 'http://xxxx.com/server/imessage/get_messages.php';


    my $req = HTTP::Request->new('POST', $uri);
    $req->header( 'Content-Type' => 'application/json' );
    $req->content( $json );
    my $lwp = LWP::UserAgent->new;
    $response = $lwp->request( $req );

    if ($response->decoded_content ne "")
    {
        print "[$datestring] ";
          my $decoded_json = decode_json($response->decoded_content);
        print $decoded_json->{"to_user"} ;    #ERROR line
    }

Open in new window


I understand I need to loop through the results and then display them individually but I cant figure out how :(

thank you
0
Comment
Question by:jellydeal
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40529423
The JSON handling looks correct but decode_json returns a ref so you need to loop over that (based on your json, it should be an arrayref).
use strict;
use warnigns;
use HTTP::Request;
use LWP::UserAgent;

my $json = encode_json \%message;
my $uri = 'http://xxxx.com/server/imessage/get_messages.php';

    my $req = HTTP::Request->new('POST', $uri);
    $req->header( 'Content-Type' => 'application/json' );
    $req->content( $json );
    my $lwp = LWP::UserAgent->new;
    $response = $lwp->request( $req );

    if ($response->decoded_content ne "") {
        print "[$datestring] ";
        my $decoded_json = decode_json($response->decoded_content);
        foreach my $ent (@$decoded_json) {
            print $ent->{"to_user"} ;
        }
    }

Open in new window

0
 

Author Closing Comment

by:jellydeal
ID: 40531615
fantastic.. worked perfect.. thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now