Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: JSON ARRAY Problem

by Riales (Hermit)
on Sep 30, 2013 at 22:21 UTC ( #1056426=note: print w/ replies, xml ) Need Help??


in reply to JSON ARRAY Problem

You should decode the JSON so you could interact with the data as Perl structures:

use JSON; my $json = <<HERE; {"id":241995,"name":"Georgia vs Finland","start":"2013-09-10T17:00:00. +000Z","status":"paid","sport-id":15,"category-id":[146598],"markets": +[],"meta-tags":[{"id":4,"name":"Soccer","type":"SPORT"},{"id":650,"na +me":"Qualifiers","type":"COMPETITION"},{"id":5,"name":"World Cup 2014 +","type":"COMPETITION"},{"id":1,"name":"Sport","type":"Root"},{"id":6 +51,"name":"September 10th 2013","type":"DATE"}],"in-running-flag":fal +se,"allow-live-betting":false} HERE my $data = decode_json($json); my @meta_tag_ids = map { $_->{id} } @{ $data->{meta_tags} };

EDIT: Actually, maybe I understand the problem now...you only want the IDs of the metatags that display the sport being played and the date the sport was played on, correct?

use JSON; my $json = <<HERE; {"id":241995,"name":"Georgia vs Finland","start":"2013-09-10T17:00:00. +000Z","status":"paid","sport-id":15,"category-id":[146598],"markets": +[],"meta-tags":[{"id":4,"name":"Soccer","type":"SPORT"},{"id":650,"na +me":"Qualifiers","type":"COMPETITION"},{"id":5,"name":"World Cup 2014 +","type":"COMPETITION"},{"id":1,"name":"Sport","type":"Root"},{"id":6 +51,"name":"September 10th 2013","type":"DATE"}],"in-running-flag":fal +se,"allow-live-betting":false} HERE my $data = decode_json($json); my @interesting_meta_tag_ids = (); foreach my $metatag (@{ $data->{meta_tags} }) { my $type = $metatag->{type}; if ($type eq 'SPORT' or $type eq 'DATE') { push @interesting_meta_tag_ids, $metatag->{id}; } }


Comment on Re: JSON ARRAY Problem
Select or Download Code
Re^2: JSON ARRAY Problem
by davido (Archbishop) on Sep 30, 2013 at 23:50 UTC

    The OP's code snippet included this line:

    my $var1 = $decoded->{'meta-tags'}[$i]{'id'};

    He's already using some JSON module to arrive at that data structure.


    Dave

      Hmm...you're right. I guess I don't understand the question then. The code I included seems to work for extracting the ID he wanted.

        I didn't understand it either. I dumped the decoded JSON, and was stumped by the part where he was looking for a specific element in the "meta" array that contained a date; the only date field I found wasn't contained within the array.


        Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2015-07-07 02:00 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 (86 votes), past polls