NickHatBoecker
NickHatBoecker

Reputation: 33

How to reference Data in an Array/Hash from JSON Response in Perl

I'm writing some Perl Code and I am a totally noob (specialized in PHP and JS). I'm retrieving some data from a JSON API Request and I want to reference the data and build some HTML-Options for a select field. But I just can't figure out how to reference the values from the JSON Response.

#!/bin/false

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use JSON;
use Encode;

use vars qw(@parameters $new $mode $metainfo);

use Data::Dumper qw(Dumper);

sub main
{
    if ($mode ne 'EDIT') {
        return;
    }

    my @pagetypes = _getPagetypes();
    if (scalar @pagetypes == 0 || ! defined \@pagetypes) {
        return "";
    }

    my $html = "";

    foreach (@pagetypes) {
        my $pagetype = $_;

        return Dumper @pagetypes;

        $html .= sprintf(
            '<option value="%s">%s</option>',
            $pagetype->{"identifier"},
            encode_utf8($pagetype->{"label"}),
        );
    }

    return $html;
}

##
# Get Pagetypes from API
##
sub _getPagetypes
{
    my $url = sprintf(
        "%s/page-types/",
        $ENV{'URL'}
    );

    my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
    my $header = HTTP::Request->new(GET => $url);
    my $request = HTTP::Request->new('GET', $url, $header);
    my $response = $ua->request($request);

    my @data = decode_json($response->content);
    my @pagetypes = @data[0]->{'pageTypes'};

    return @pagetypes;
}

Where it says return Dumper $pagetypes; the following output is written:

$VAR1 = [
    {
        'label' => 'Projektsteckbrief',
        'identifier' => 'pagetype-profile'
    }
];

I tried $pagetype->{"label"} and $pagetypes[0]->{"label"} and so much more...but I just don't know how to get the label and the identifier there.

update:

I tried to implement the pagetypes manually and this is working fine with the main() method (without the Dumper Call):

sub _getPagetypes
{
    my @pagetypes = (
        {
            identifier => "pagetype-profile",
            label => "Projektsteckbrief",
        },
    );

    return @pagetypes;
}

UPDATE 2:

So I got a new problem...it's not working completely yet, but Bl00D4NGEL's response was a great help anyway!

So my code looks like this right now:

#!/bin/false

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use JSON;
use Encode;
use Project::Util::Api;

use vars qw(@parameters $new $mode $metainfo);

sub main
{
    if ($mode ne 'EDIT') {
        return;
    }

    my @pagetypes = _getPagetypes();
    if (scalar @pagetypes == 0 || ! defined \@pagetypes) {
        return "";
    }

    my $html = "";

    foreach my $pagetype (@pagetypes) {
        # selection will be automatically set by Imperia
        $html .= sprintf(
            '<option value="%s">%s</option>',
            $pagetype->[0]->{"identifier"},
            encode_utf8($pagetype->[0]->{"label"}),
        );
    }

    return $html;
}

##
# Get Pagetypes from API
##
sub _getPagetypes
{
    my $response = Project::Util::Api->sendRequest("/ajax/imperia/page-types/de/");
    my @pagetypes = $response->{"pageTypes"};

    return @pagetypes;
}

$new = main();

The problem is now that only one item is displayed. So $pagetype in my foreach loop looks like it's still the array with all of the items inside and $pagetype->[0] then gives me only the first item out of the array.

When I use for instead of foreach like this:

for ( $a = 0 ; $a <= 10 ; $a++ ) {
    $html .= sprintf(
        '<option value="%s">%s</option>',
        @pagetypes[0]->[$a]->{"id"},
        encode_utf8(@pagetypes[0]->[$a]->{"label"}),
    );
}

I get 10 items of 200, but I just cant figure out how to get the length of the array @pagetypes. I tried the following:

# 500 Server error (because of long loading?)
for ( $a = 0 ; $a <= @pagetypes[0] ; $a++ ) {

my $pagetypesLength = @pagetypes;
# Only 2 items are displayed
for ( $a = 0 ; $a <= @pagetypes ; $a++ ) {
for ( $a = 0 ; $a <= $pagetypesLength ; $a++ ) {
for ( $a = 0 ; $a <= scalar @pagetypes ; $a++ ) {
for ( $a = 0 ; $a <= length @pagetypes ; $a++ ) {

UPDATE 3 (FINALLY WORKING):

So finally I managed to get it working. Final code following:

#!/bin/false

use strict;
use warnings;

use Encode;
use Project::Util::Api;

use vars qw(@parameters $new $mode $metainfo);

sub main
{
    if ($mode ne 'EDIT') {
        return;
    }

    my $items = _getData();
    if (0 == scalar @$items) {
        return "";
    }

    my $html = "";

    # $#{$items} is last index of array reference $items
    for (my $i = 0 ; $i <= $#{$items} ; $i++) {
        if (defined $items->[$i]->{"id"}) {
            # option selection will be automatically set by Imperia
            $html .= sprintf(
                '<option value="%s">%s</option>',
                $items->[$i]->{"identifier"},
                encode_utf8($items->[$i]->{"label"}),
            );
        }
    }

    return $html;
}

##
# Get Pagetypes from API
##
sub _getData
{
    my $response = Project::Util::Api->sendRequest("/ajax/imperia/page-types/de/");

    return $response->{"pageTypes"};
}

$new = main();

Upvotes: 0

Views: 602

Answers (1)

Bl00D4NGEL
Bl00D4NGEL

Reputation: 363

It looks like the $pagetype variable is actually an array ref (looking at the Dumper at least it looks like it) So what should probably solve this issue is this:

    my $pagetype = $_;

    $html .= sprintf(

    '<option value="%s">%s</option>',
    $pagetype->[0]->{"identifier"},
    encode_utf8($pagetype->{"label"}),

    );

Upvotes: 1

Related Questions