Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Good job searching to find this old thread! Your question is not related to JSON, but rather to perl data structures. See perlref.

You should get in the habit of using Data::Dumper to examine your complex data structures. In this case, you still were unclear on how to access the nested data, but examining the structure is always the first step.

(Note that jsonToObject in the OP is deprecated.)

Here's an example that shows how to get your arrays. It's commented so you can see what's going on, and it deliberately uses more steps than optimal to be clear (if you really wanted to build separate arrays, you should probably use map).

#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; use JSON; my $data = qq# { "items" : [ { "name" : "Theodor Nelson", "id": "_333301" }, { "name": "Morton Heilig", "id": "_13204" } ] } #; my $obj = from_json( $data ); my @names; my @ids; say Dumper $obj; # $obj is an anonymous hash with one key, 'items' my @items = @{ $obj->{'items'} }; # The value of the key 'items' is an anonymous array. # Here for simplicity we copy it into a new array. # We access it by deferencing it using @{ } foreach my $item ( @items ) { # Each element of the anonymous array is a hashref. # So $item is a hashref, not a scalar my $name = $item->{'name'}; my $id = $item->{'name'}; push @names, $name; push @ids, $id; } say 'Names: ' . join ', ', @names; say 'IDs: ' . join ', ', @ids; __END__
A more idiomatic way to put the data into a hash keyed by ID as you suggested above:
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; use JSON; my $data = qq# { "items" : [ { "name" : "Theodor Nelson", "id": "_333301" }, { "name": "Morton Heilig", "id": "_13204" } ] } #; my $obj = from_json( $data ); my %names; $names{ $_->{'id'} } = $_->{'name'} for @{ $obj->{'items'} }; while ( my( $id, $name ) = each %names ) { say "$id: $name"; } __END__
Output:
$ perl 651544-2.pl _13204: Morton Heilig _333301: Theodor Nelson $
And finally, how to access the names directly in the object:
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; use JSON; my $data = qq# { "items" : [ { "name" : "Theodor Nelson", "id": "_333301" }, { "name": "Morton Heilig", "id": "_13204" } ] } #; my $obj = from_json( $data ); say ${ $obj->{'items'}->[1] }{'name'}; # ^ ^ ^ ^ ^ ^ # | | | | | | # | is an arrayref | ----------- # | | | # | first element of which is a hash | # | | # the variable is value of the 'name' key __END__
Hope this helps!

The way forward always starts with a minimal test.

In reply to Re^3: JSON and Perl Objects - How to access data? by 1nickt
in thread JSON and Perl Objects - How to access data? by harryhalpin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found