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

Extracting data using JSON

by Shaveta_Chawla (Sexton)
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???

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-05-26 23:52 GMT
Find Nodes?
    Voting Booth?