use warnings; use strict; use Data::Dump; use JSON::Path; use JSON; my $data = decode_json(<<'ENDJSON'); {"status":"ok","message-type":"work-list","message-version":"1.0.0", "message":{"facets":{},"total-results":1,"items": [ {"author":[ {"given":"Brahim","family":"Oubaha","sequence":"first","affiliation":[ {"name":"Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco"}, {"name":"Laboratory of Interaction Plant\u2010Microorganisms, Department of Biology University of Fribourg Fribourg Switzerland"} ]}, {"given":"Ahmed","family":"Nafis","sequence":"additional","affiliation":[ {"name":"Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco"} ]}, {"given":"Mohamed","family":"Baz","sequence":"additional","affiliation":[ {"name":"Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco"} ]}, {"given":"Felix","family":"Mauch","sequence":"additional","affiliation":[ {"name":"Laboratory of Interaction Plant\u2010Microorganisms, Department of Biology University of Fribourg Fribourg Switzerland"}]}, {"given":"Mustapha","family":"Barakate","ORCID":"http:\/\/orcid.org\/0000-0001-7949-5592","authenticated-orcid":false,"sequence":"additional","affiliation":[ {"name":"Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco"}, {"name":"Agrobiosciences & Fertilizers Program University Mohammed IV Polytechnic (UM6P) Benguerir Morocco"}]} ],"title":["The potential of antagonistic moroccan\n Streptomyces\n isolates for the biological control of damping\u2010off disease of pea (\n Pisum sativum\n L.) caused by\n Aphanomyces euteiches"]} ], "items-per-page":20,"query":{"start-index":0,"search-terms":null}}} ENDJSON dd unfoldauthors($data->{message}{items}[0]{author}); dd unfoldauthors2($data->{message}{items}[0]{author}); dd unfoldauthors3($data->{message}{items}[0]{author}); sub unfoldauthors { my ($data_ar) = @_; return unless ($data_ar); $data_ar = $data_ar->[0]; my @paths = qw( $..family $..given $..affiliation ); my @selectors; for my $p (@paths) { push @selectors, JSON::Path->new($p); } my $aff_name = JSON::Path->new('$..name'); my $name; my @lines; my @sep = ( ", ", "; ", " " ); my $col = 0; for my $s (@selectors) { my @text = $s->values($data_ar); my $authors_limit = 50; my $last = $authors_limit - 1; if ( @text > $authors_limit ) { @text = @text[ 0 .. $last ]; $text[$last] .= " ..." if ( $col == 1 ); } my $pos = 0; for my $name (@text) { #print "$col $name\n"; if ( $col == 2 ) { my @aff = $aff_name->values( $text[$pos] ); $name = join( " - ", @aff ); } $lines[ $pos++ ] .= $name . $sep[$col]; } $col++; } return \@lines; } sub unfoldauthors2 { my ($authors) = @_; return [ $authors->[0]{family}.', '.$authors->[0]{given}.'; '. join(' - ', map {$_->{name}} @{$authors->[0]{affiliation}}) ]; } sub unfoldauthors3 { my ($authors) = @_; return [ map { $_->{family}.', '.$_->{given}.'; '.join(' - ', map {$_->{name}} @{ $_->{affiliation} }) } @$authors ]; } __END__ [ "Oubaha, Brahim; Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco - Laboratory of Interaction Plant\x{2010}Microorganisms, Department of Biology University of Fribourg Fribourg Switzerland ", ] [ "Oubaha, Brahim; Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco - Laboratory of Interaction Plant\x{2010}Microorganisms, Department of Biology University of Fribourg Fribourg Switzerland", ] [ "Oubaha, Brahim; Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco - Laboratory of Interaction Plant\x{2010}Microorganisms, Department of Biology University of Fribourg Fribourg Switzerland", "Nafis, Ahmed; Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco", "Baz, Mohamed; Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco", "Mauch, Felix; Laboratory of Interaction Plant\x{2010}Microorganisms, Department of Biology University of Fribourg Fribourg Switzerland", "Barakate, Mustapha; Laboratory of Biology and Biotechnology of Microorganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocco - Agrobiosciences & Fertilizers Program University Mohammed IV Polytechnic (UM6P) Benguerir Morocco", ]