Solved

parse json with perl

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl getopt long help 34 72
ejb entity bean example 2 17
Java syntax, or is it Selenium 6 30
AWK: add from two digits (the month) within date string dd/mm/yyyy 2 18
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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