Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

HOw to extract data using json

by Priti24 (Novice)
on Dec 08, 2011 at 04:58 UTC ( #942354=perlquestion: print w/ replies, xml ) Need Help??
Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

i have a variable like $list having some value

$list = "res":{ "url":"http://www.astm.org/FULL_TEXT/D5447/HTML/D5447. +htm", "title":"Standard Guide for Application of a Groundwater Flow M +odel to a Site-Specific Problem ", "snippet":"1.1 This guide covers t +he application and subsequent documentation of a groundwater flow mod +el to a particular site or problem. In this context, x201C; groundwat +er flow model x201D; refers to the application of a mathematical mode +l to the solution of a site-specific groundwater flow problem. 1.2 Th +is guide illustrates the major steps to take in developing a groundwa +ter flow model that ", "meta":{"gs_designation":"D5447","mc_section": +"04","mc_maincomm":"D18","mc_date":"2010","mc_dltype":"allstd,active" +,"mc_doctype":"Active Standard","mc_addtocart":"PDF-D5447","mc_login" +:"true","mc_suburl":"/SUBSCRIPTION/filtrexx40.cgi?+/usr6/htdocs/newpi +lot.com/SUBSCRIPTION/REDLINE_PAGES/D5447.htm","mc_relatedurl":"D5447_ +related.htm","mc_keywords":" computer model~ groundwater~ simulation~ + Ground-water model~ ICS Number Code 13.060.10 (Water of natural reso +urces)","gs_year":"04(2010)","gs_collection":"EEE,EEE,OOO","gs_homebo +ok":"0408 CS04" } }}

i want to extract url ,title ,date from this variable. please help me how could this be possible using JSON.

Comment on HOw to extract data using json
Download Code
Re: HOw to extract data using json
by Anonymous Monk on Dec 08, 2011 at 05:12 UTC

    Using the module JSON?

    Have you read perlintro yes? You should read perlintro, Quote and Quote like Operators, Tutorials: References quick reference

    #!/usr/bin/perl -- use strict; use warnings; use JSON; Main( @ARGV ); exit( 0 ); sub Main { my $json = <<'__JSON__'; {"res": { "url":"http://www.astm.org/FULL_TEXT/D5447/HTML/D5447.htm", +"title":"Standard Guide for Application of a Groundwater Flow Model t +o a Site-Specific Problem ", "snippet":"1.1 This guide covers the app +lication and subsequent documentation of a groundwater flow model to +a particular site or problem. In this context, x201C; groundwater flo +w model x201D; refers to the application of a mathematical model to t +he solution of a site-specific groundwater flow problem. 1.2 This gui +de illustrates the major steps to take in developing a groundwater fl +ow model that ", "meta":{"gs_designation":"D5447","mc_section":"04"," +mc_maincomm":"D18","mc_date":"2010","mc_dltype":"allstd,active","mc_d +octype":"Active Standard","mc_addtocart":"PDF-D5447","mc_login":"true +","mc_suburl":"/SUBSCRIPTION/filtrexx40.cgi?+/usr6/htdocs/newpilot.co +m/SUBSCRIPTION/REDLINE_PAGES/D5447.htm","mc_relatedurl":"D5447_relate +d.htm","mc_keywords":" computer model~ groundwater~ simulation~ Groun +d-water model~ ICS Number Code 13.060.10 (Water of natural resources) +","gs_year":"04(2010)","gs_collection":"EEE,EEE,OOO","gs_homebook":"0 +408 CS04" } }} __JSON__ my $perl_scalar = JSON->new->decode($json); print JSON->new->utf8(1)->pretty(1)->encode($perl_scalar); print '#' x 6, , "\n"; print $perl_scalar->{res}{title}, "\n"; } __END__ { "res" : { "snippet" : "1.1 This guide covers the application and subsequen +t documentation of a groundwater flow model to a particular site or p +roblem. In this context, x201C; groundwater flow model x201D; refers +to the application of a mathematical model to the solution of a site- +specific groundwater flow problem. 1.2 This guide illustrates the maj +or steps to take in developing a groundwater flow model that ", "url" : "http://www.astm.org/FULL_TEXT/D5447/HTML/D5447.htm", "title" : "Standard Guide for Application of a Groundwater Flow +Model to a Site-Specific Problem ", "meta" : { "mc_date" : "2010", "gs_collection" : "EEE,EEE,OOO", "gs_homebook" : "0408 CS04", "mc_section" : "04", "mc_login" : "true", "mc_maincomm" : "D18", "mc_keywords" : " computer model~ groundwater~ simulation~ Gr +ound-water model~ ICS Number Code 13.060.10 (Water of natural resourc +es)", "gs_year" : "04(2010)", "mc_dltype" : "allstd,active", "mc_relatedurl" : "D5447_related.htm", "gs_designation" : "D5447", "mc_addtocart" : "PDF-D5447", "mc_suburl" : "/SUBSCRIPTION/filtrexx40.cgi?+/usr6/htdocs/new +pilot.com/SUBSCRIPTION/REDLINE_PAGES/D5447.htm", "mc_doctype" : "Active Standard" } } } ###### Standard Guide for Application of a Groundwater Flow Model to a Site-S +pecific Problem
      Thank you so much for your help. But can we generalize it? i.e I have an array carrying 10 lines similar to $lines as above, i want to extract data from all the lines. can i use the array variable instead of direct value.

        Thank you so much for your help. But can we generalize it? i.e I have an array carrying 10 lines similar to $lines as above, i want to extract data from all the lines. can i use the array variable instead of direct value.

        See How do I post a question effectively?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-09-24 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (243 votes), past polls