Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

parsing JSON for specific entries

by tevolo (Novice)
on Aug 01, 2012 at 18:33 UTC ( [id://984872]=perlquestion: print w/replies, xml ) Need Help??

tevolo has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise monks.

I have a very large json formatted file that I am trying to extract specific values from. The json file looks like such:

{ "Content_ID" : { "CNBP_021687" : { "Title" : "Molecule of the Week - + Pyruvic Acid", "Type" : "Article", "Web Extension" : "doc", "WebSite" : "PublicWebSite", "WebSiteSection" : "PublicWebSite:841" }, "CNBP_021688" : { "Title" : "STEM Education Coalition Supports V +olunteer Service Legislation", "Type" : "Article", "Web Extension" : "pdf", "WebSite" : "PublicWebSite", "WebSiteSection" : "PublicWebSite:1892" }, "CNBP_021691" : { "Title" : "WCC Lectureship Award", "Type" : "Article", "Web Extension" : "doc", "WebSite" : "PublicWebSite", "WebSiteSection" : "PublicWebSite:1367" },

And my script looks like such:

#!c:/strawberry/perl/bin/perl.exe use JSON; use Data::Dumper; use warnings; use strict; use utf8; use URI::Escape; my $text; my $json =""; my $object = ""; my $item = ""; sub extract_json { my $file = shift; local $/; #enable slurp open my $fh, "<", "$file"; $json = <$fh>; return $json; } my $jobj = extract_json('C:\Temp3\articlePaagesJson.txt'); use Encode; $object = JSON::XS->new->decode (decode "UTF-8", $jobj); # print Dumper $object foreach $item (@{$object->{Content_ID}} ){ print " do nothing \n"; };

When I use data dumber the values print but I keep getting an error Not an ARRAY reference at

I am trying to exctract the content ID the title and the web extension if that helps at all. I keep trying everything and anything I see online and on the board but nothing is working.

Any help would be greatly appreciated

Replies are listed 'Best First'.
Re: parsing JSON for specific entries
by suaveant (Parson) on Aug 01, 2012 at 19:04 UTC
    { denotes a hash, [ denotes array, just like in Perl. You are trying to iterate through $object->{Content_ID} by dereferencing it as an ARRAY but it is a hash, with keys CNBP_021687, CNBP_021688 and CNBP_021691. The correct way to iterate would be:
    foreach $key (keys %{$object->{Content_ID}} ){ my $item = $object->{Content_ID}{$key}; print " do nothing \n"; };

                    - Ant
                    - Some of my best work - (1 2 3)

Re: parsing JSON for specific entries
by linuxkid (Sexton) on Aug 02, 2012 at 01:29 UTC

    just out of curoisty, could you use YAML?

    --linuxkid


    imrunningoutofideas.co.cc

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found