0

How to fill list with JSON data?

Here's my code:

my $groups = get_groups($t);
my @group;
my $i = 0;
do {
    push(@group, {
        groups  => [
            { type => $groups->{groups}->[$i]->{type} , group => $groups->{groups}->[$i]->{group} },
        ]
    });
    $i++;
} while ($i < length $groups->{groups});

Here is the json sample:

{
    "error":false,
    "message":"success",
    "group":[
        {"type":1,"group":"group1"},
        {"type":2,"group":"group2"},
        {"type":3,"group":"group3"},
        {"type":4,"group":"group4"},
        {"type":5,"group":"group5"}
    ]
}

Function get_groups($t); will return above json. I want to get the array group and put it into list groups. But I got:

Can't use string ("0") as a HASH ref while "strict refs" in use

5
  • Again, please include your data in the question. Also, you might not want to use a do ... while construct, it's quite un-Perlish and hard to read. And there's no more JSON in this. JSON is irrelevant now. It's now just data. Commented Sep 21, 2018 at 14:02
  • length $opsteams->{teams} is probably not what you want. That returns the character length of a string, not the element length of an array. Try scalar @{$opstreams->{teams}} Commented Sep 21, 2018 at 14:05
  • @simbabque updated the json sample. Commented Sep 21, 2018 at 14:13
  • 1
    As I said, you need to distinguish between JSON and Perl data structures. In the code you've shown here, there is no more JSON. You've already taken care of converting that to Perl. While I can work with the JSON data structure, it's important for you to understand the difference if you want to learn how to do all of this on your own. Commented Sep 21, 2018 at 15:44
  • @bluepanther: It seems that you are struggling a bit. You've posted a few questions recently and in each case, you don't really seem to have understood the answers you've received to previous questions. I recommend spending a few hours going through this carefully and ensuring that you understand what is going on here. For example, do you know the difference between JSON and a Perl data structure that has been "decoded" from JSON? Understanding the fundamentals is important. Commented Sep 21, 2018 at 16:14

1 Answer 1

2

From the documentation of length:

Returns the length in characters of the value of EXPR. If EXPR is omitted, returns the length of $_ . If EXPR is undefined, returns undef.

This function cannot be used on an entire array or hash to find out how many elements these have. For that, use scalar @array and scalar keys %hash , respectively.

To get the number of elements in an array reference, you need to dereference it and put it into scalar context.

my $foo = [ qw/a b c/ ];
my $number_of_elements = scalar @{ $foo }; # 3

What you actually want to do is loop over every team in the teams array. No need to get the number of elements.

my @teams;
foreach my $team ( @{ $opsteams->{teams} } ) {
    push @teams, {
        type => $team->{type},
        team => $team->{team},
    };
}

There are some extra layers of depth in your code. I'm not sure what they are for. It actually looks like you just want the teams in @teams, which really would be

my @teams = @{ $opsteams->{teams} };
Sign up to request clarification or add additional context in comments.

Comments

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.