Solved

parse json with perl

Posted on 2015-01-03
2
594 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

759 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

24 Experts available now in Live!

Get 1:1 Help Now