0

I have to parse a JSON structure, which contains an unknown element name.

Normally I would access the data something like this.

my $layer1 = $responselayer->{'data'};
my $layer2 = $layer2->{'123'};
my $layer3 = $layer3->{'Details'};
print $layer3->{'name'};

Since the details number (123 / 945 / 345) is repeated in a random order (although unique in this case), I do not know how to address the data on 'Details' level.

"data":
    {
    "123":
        {
        "Details": 
            {
            "name":"Jake",
            "description":"aaa",
            }
        },
    "945":
        {
        "Details":
            {
            "name":"Jim",
            "description":"bbb",
            }
        },
    "345":
        {
        "Details":
            {
            "name":"Bob",
            "description":"ccc",
            }
        }
    }

Thanks!

1 Answer 1

2

You'll have to loop over $layer1 hashref values,

my $layer1 = $responselayer->{'data'};
for my $layer2 (values %$layer1) {

  print $layer2->{'Details'}{'name'}, "\n";
}

in case you need to know what are the corresponding keys,

my $layer1 = $responselayer->{'data'};
for my $key (keys %$layer1) {

  my $layer2 = $layer1->{$key};
  print $layer2->{'Details'}{'name'}, "\n";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Tried it immediately. Works perfect. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.