Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^4: JSON::Path and node iteration (updated)

by frazap (Monk)
on Mar 22, 2019 at 06:53 UTC ( [id://1231564] : note . print w/replies, xml ) Need Help??


in reply to Re^3: JSON::Path and node iteration (updated)
in thread JSON::Path and node iteration

Yes
sub unfoldauthors3 { my ($authors) = @_; return [ map { $_->{family}.', '.$_->{given}.'; '.join(' - ', map {$_->{name}} @{ $_->{affiliation} }) } @{$authors->[0]} ]; }
does it.

And I agree that my original unfoldauthors using JSON::Path is overkilling.

My original question should have been reworded as:

If I wanted to use JSON::Path instead of ripping the perl data directly could I have the same output in one extraction, in one loop ?

In the unfoldauthors above I have to use the JSON::Path object for the affiliation name for each author's name.

Is there a way using JSON::Path to have in the same time, the author's family, given, affiliation.name ?

Replies are listed 'Best First'.
Re^5: JSON::Path and node iteration (updated)
by hdb (Monsignor) on Mar 22, 2019 at 14:15 UTC

    I guess this is also not quite what you wanted...

    my @all = JSON::Path->new( '$..author[*].[family,given,affiliation]' ) +->values( $data ); print Dumper \@all; __END__ $VAR1 = [ 'Oubaha', 'Brahim', [ { 'name' => 'Laboratory of Biology and Biotechnology of Mi +croorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marra +kech Morocco' }, { 'name' => "Laboratory of Interaction Plant\x{2010}Microo +rganisms, Department of Biology University of Fribourg Fribourg Switz +erland" } ], 'Nafis', 'Ahmed', [ { 'name' => 'Laboratory of Biology and Biotechnology of Mi +croorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marra +kech Morocco' } ], 'Baz', 'Mohamed', [ { 'name' => 'Laboratory of Biology and Biotechnology of Mi +croorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marra +kech Morocco' } ], 'Mauch', 'Felix', [ { 'name' => "Laboratory of Interaction Plant\x{2010}Microo +rganisms, Department of Biology University of Fribourg Fribourg Switz +erland" } ], 'Barakate', 'Mustapha', [ { 'name' => 'Laboratory of Biology and Biotechnology of Mi +croorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marra +kech Morocco' }, { 'name' => 'Agrobiosciences & Fertilizers Program Uni +versity Mohammed IV Polytechnic (UM6P) Benguerir Morocco' } ] ];

      Nice !

      my $cr = REST::Client::CrossRef->new( mailto => 'd...h', spit_raw_data => 0, add_end_flag => 1, json_path => [ ['$..author[*].[family,given,affiliation]'], ], json_path_callback => { '$..author[*].[family,given,affiliation]' => \&unfoldauthors +4 }, ); sub unfoldauthors4 { my ($authors) = @_; my @all = @{$authors}; print Dumper @all; my $authors_limit = 50; my $last = $authors_limit - 1; my $size = @all / 3; if ( $size > $authors_limit ) { @all = @all[ 0 .. $last * 3 ]; $all[ $last * 3 - 1 ] .= " ..."; } my @res; my $i; for ( $i = 0; $i <= $#all; $i += 3 ) { my $line = $all[$i] . ", " . $all[ $i + 1 ] . "; " . join( " ", map { $_->{name} } @{ $all[ $i + 2 ] } ); push @res, $line; } return \@res; }

      works

      Thanks

      Update the above loop is not elegant and perlish; This is easier to read (without the truncation to 50 authors:
      sub unfoldauthors4 { my ($authors) = @_; my @all = @{$authors}; my @res; while (@all) { push @res, shift( @all) . ", ". shift( @all) . "; " . join(" ", +map {$_->{name}} @{ shift( @all )}); } return \@res; }