G'day Gruaig,

Welcome to the monastery.

Your description of what you're trying to extract doesn't make sense. You say what you want "has the date and sports name in it". You then show two elements: the first has "name":"World Cup 2014" (which is possibly related to some sport and has a year); the second has "name":"September 10th 2013" (which clearly has a date but makes no reference to any sport).

The code you've posted is not really of much help. It's incomplete and, as such, can't be run to reproduce what you're running; nor do we know what else it is supposed to be doing (we can only guess at the code completing the if statement and for loop). Furthermore, including line numbers (as part of the code) is also unhelpful: we'd need to remove them to run the code.

This code shows how to access the meta-tags elements and conditionally extract them:

#!/usr/bin/env perl use strict; use warnings; use JSON; use Data::Dumper; my $data = decode_json '{"id":241995,"name":"Georgia vs Finland","star +t":"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,"name":"Qualifiers","type":"COMPETITION"},{"id": +5,"name":"World Cup 2014","type":"COMPETITION"},{"id":1,"name":"Sport +","type":"Root"},{"id":651,"name":"September 10th 2013","type":"DATE" +}],"in-running-flag":false,"allow-live-betting":false}'; my @extract; meta_wanted($_) && push @extract, $_ for @{$data->{'meta-tags'}}; print Dumper \@extract; sub meta_wanted { $_[0]->{type} eq 'SPORT' || $_[0]->{type} eq 'DATE' }

Output:

$VAR1 = [ { 'type' => 'SPORT', 'id' => 4, 'name' => 'Soccer' }, { 'name' => 'September 10th 2013', 'id' => 651, 'type' => 'DATE' } ];

You'll need to define meta_wanted() how you want it: what I have there is just a guess and likely to be not what you're after.

If you need to ask a follow-up question, please read the guidelines in "How do I post a question effectively?" before doing so.

-- Ken


In reply to Re: JSON ARRAY Problem by kcott
in thread JSON ARRAY Problem by Gruaig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.