Flip76 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I'm stucked and can't find any solution. Since 3 days I am researching all kind of forums/documentation but I can't get any hint.
Here is my problem:
I'm requesting data from a REST-API and I'm receiving JSON-Format. Now I want to build a Dereference (e. g. $item->{meta}->{created}) dynamically but it does only work for the 1st level ($item->{userName}).
Maybe you can help relieving my headache?
Regards,
Flip
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Code Sniplet #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ my $data_structure = decode_json($client->responseContent()); print Dumper($data_structure); # Output of print Dumper($data_structure); $VAR1 = { 'Resources' => [ 'displayName' => 'User 1', 'userName' => 'user1@test.com', 'id' => '1234567', 'meta' => { 'created' => '2018-04-16T11:5 +7:19.376Z' } }, { 'displayName' => 'User 2', 'userName' => 'user2@test.com', 'id' => '1234568', 'meta' => { 'created' => '2018-04-16T11:5 +9:27.111Z' } }, { 'displayName' => 'User 3', 'userName' => 'user3@test.com', 'id' => '12345679', 'meta' => { 'created' => '2018-11-21T14:4 +9:33.821Z' } }, ], 'totalResults' => 3, 'itemsPerPage' => 50, 'startIndex' => 1, 'schemas' => [ 'urn:ietf:params:scim:api:messages:2.0:ListRe +sponse' ] }; # Normally @elements is filled by entries from a config-file # but for make it easy, I set the values here my @elements; push(@elements,"userName"); push(@elements,"displayName"); push(@elements,"meta.created"); foreach $item ( @{ $data_structure->{Resources} } ) { foreach $element (@elements) { # Convert a . to }->{ in order for meta.created to look like m +eta}->{created $element =~ s/\./}->{/g; # This works for all elements except of meta}->{created $deref = $item->{$element}; print "Convert: \$item->{" . $element . "}\t\t\t" . $deref . +"\n"; } # But this works deref = $item->{meta}->{created}; print "When use direct: \$item->{meta}->{created}\t" . $deref . "\ +n"; print "\n"; } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Output: #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Convert: $item->{userName} user1@test.com Convert: $item->{displayName} User 1 Use of uninitialized value $deref in concatenation (.) or string at /h +ome/dczesak/cis4oci/cis4oci.pl line 291. Convert: $item->{meta}->{created} When use direct: $item->{meta}->{created} 2018-04-16T11:57:19.37 +6Z Convert: $item->{userName} user2@test.com Convert: $item->{displayName} User 2 Use of uninitialized value $deref in concatenation (.) or string at /h +ome/dczesak/cis4oci/cis4oci.pl line 291. Convert: $item->{meta}->{created} When use direct: $item->{meta}->{created} 2018-04-16T11:59:27.11 +1Z Convert: $item->{userName} user3@test.com Convert: $item->{displayName} User 3 Use of uninitialized value $deref in concatenation (.) or string at /h +ome/dczesak/cis4oci/cis4oci.pl line 291. Convert: $item->{meta}->{created} When use direct: $item->{meta}->{created} 2018-11-21T14:49:33.82 +1Z
2018-12-08 Athanasius changed <pre> to <p> tags around text
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamic Dereferencing
by haukex (Archbishop) on Dec 07, 2018 at 11:32 UTC | |
by Flip76 (Novice) on Dec 07, 2018 at 13:15 UTC | |
by poj (Abbot) on Dec 07, 2018 at 13:42 UTC | |
|
Re: Dynamic Dereferencing
by 1nickt (Canon) on Dec 07, 2018 at 12:08 UTC | |
by Flip76 (Novice) on Dec 07, 2018 at 13:11 UTC | |
by Flip76 (Novice) on Dec 07, 2018 at 13:39 UTC | |
|
Re: Dynamic Dereferencing
by bliako (Abbot) on Dec 07, 2018 at 11:32 UTC | |
by Flip76 (Novice) on Dec 07, 2018 at 12:43 UTC | |
by bliako (Abbot) on Dec 07, 2018 at 12:55 UTC | |
by Flip76 (Novice) on Dec 07, 2018 at 13:13 UTC | |
|
Re: Dynamic Dereferencing
by markong (Pilgrim) on Dec 07, 2018 at 12:10 UTC | |
by Anonymous Monk on Dec 07, 2018 at 12:25 UTC | |
by markong (Pilgrim) on Dec 07, 2018 at 14:41 UTC |