Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Parsing REST API JSON

by welle (Beadle)
on May 13, 2012 at 15:40 UTC ( #970294=perlquestion: print w/ replies, xml ) Need Help??
welle has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to parse the result of a REST API query. The result looks like the following:

{"responseData":{"translatedText":"Ciao mondo"},"responseDetails":""," +responseStatus":200,"matches":[{"id":"424913311","segment":"Hello Wor +ld","translation":"Ciao mondo","quality":"74","reference":"","usage-c +ount":50,"subject":"All","created-by":"","last-updated-by":null,"crea +te-date":"2011-12-29 19:14:22","last-update-date":"2011-12-29 19:14:2 +2","match":1},{"id":"0","segment":"Hello World","translation":"Ciao a + tutti","quality":"70","reference":"Machine Translation provided by G +oogle, Microsoft, Worldlingo or the MyMemory customized engine.","usa +ge-count":1,"subject":"All","created-by":"MT!","last-updated-by":null +,"create-date":"2012-05-13","last-update-date":"2012-05-13","match":0 +.85}]}

I am parsing it in the following simple way, unfortunatelly I am doing something wrong...

use JSON qw( decode_json ); # From CPAN use Data::Dumper my $json_text= "...."; my $decoded_json = decode_json( $json_text ); #print Dumper $decoded_json; foreach my $value ( @{ $decoded_json->{'responseData'} } ) { print my $value->{'translatedText'} ; }

It should be something stupid I am missing...

Comment on Parsing REST API JSON
Select or Download Code
Re: Parsing REST API JSON
by moritz (Cardinal) on May 13, 2012 at 16:08 UTC
    $decode_json->{'responseData'} is a reference to a hash, not to an array, so doing @{ $decoded_json->{'responseData'} } is bound to fail. So don't do that.

    Since you haven't told us what you want to achieve, I can't tell you how to do what you want.

      I am just trying to "extract" the value of "translatedText". (later maybe other values)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970294]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-10-23 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls