Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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":"http://www.abc.org/", "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":"http://www.abc.org/", "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

    Shaveta_Chawla:

    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

    ...roboticus

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2015-07-29 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls