?
Solved

parse json with perl

Posted on 2015-01-03
2
Medium Priority
?
896 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 2000 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

770 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