Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Extracting data using JSON

by Shaveta_Chawla (Acolyte)
on Dec 08, 2011 at 09:54 UTC ( #942384=perlquestion: print w/ replies, xml ) Need Help??
Shaveta_Chawla has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have an array carrying 10 values one of the value is:

$var = "res":{ "url":"", "title":"extracting data using JSON ", "snippet":"valuable information using JSON ", "meta":{"xy_suburl":"/abstract/123.htm","xy_keywords":" JSON","hj_year":"2009" }}
i used the code:
#!/usr/bin/perl -- use strict; use warnings; use JSON; Main( @ARGV ); exit( 0 ); sub Main { my $json = <<'__JSON__'; {"res":{ "url":"", "title":"extracting data using J +SON ", "snippet":"valuable information using JSON ", "meta":{"xy_subu +rl":"/abstract/123.htm","xy_keywords":" JSON","hj_year":"2009" }}} __JSON__ my $perl_scalar = JSON->new->decode($json); print JSON->new->utf8(1)->pretty(1)->encode($perl_scalar); print $perl_scalar->{res}{title}, "\n"; } __END__

But using this i am able to get data of one line, but i need to generalize it. i want data from all the lines i.e from the array @var. How can i do it???

Comment on Extracting data using JSON
Download Code
Replies are listed 'Best First'.
Re: Extracting data using JSON
by roboticus (Chancellor) on Dec 08, 2011 at 11:23 UTC


    You've already covered arrays in your course, right? Since your code works, you've already managed to get the JSON data into a scalar variable $perl_scalar. So now you can just use the same code you would normally use to put the value into or get it from an array.

    my @var; ... read JSON data into $perl_scalar $var[3] = $perl_scalar; # Put your JSON value into the array my $another_scalar = $var[3]; # Retrieve the value


    When your only tool is a hammer, all problems look like your thumb.

Re: Extracting data using JSON
by Corion (Pope) on Dec 08, 2011 at 09:58 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2015-11-30 01:26 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (755 votes), past polls