-1

I have a JSON response, which I need to process in Perl to extract information out of and do further processing. The JSON document looks like this:

{
  "SITE_1": [
    {
      "values": [
        {
           "time": 20170616100000,
           "v":    11
        }
      ]
    }
  ],
  "SITE_2": [
    {
      "values": [
        {
           "time": 20170616100000,
           "v":    12
        }
     }
  ]
}

I'm trying to process it in a subroutine using:

my ($ref) = @_;
foreach my $row (0..$#{$ref}) {
   $val = ${$ref}{$site}[0]{values}[0]{v}; 
   Prt('-O',"$val\n");    etc.. etc...

Getting the "Not an Array error" I think due to the first item in the JSON being in a { } not [ ].

What's the simplest way to parse the data?

ikegami
  • 367,544
  • 15
  • 269
  • 518

1 Answers1

2

Are you asking how to iterate over the elements of a hash?

for my $site (keys(%$ref)) {
   ...
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Thankyou! I am. And it worked. I combined it with a previous response to get to get exactly what I was after. – Ben Harrison Jun 16 '17 at 05:19
  • 2
    @BenHarrison [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – mkHun Jun 16 '17 at 09:31