Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: JSON::Path and node iteration

by frazap (Monk)
on Mar 21, 2019 at 15:46 UTC ( [id://1231552]=note: print w/replies, xml ) Need Help??


in reply to Re: JSON::Path and node iteration
in thread JSON::Path and node iteration

Thanks for helping!

I want to extract the names and affiliations with the format name, firstname; affiliation1 - affiliation2 - ...

I wanted all the authors, not the first. The unfoldauthors code take only the first 50, but I should have removed these line. I think that my values are in the first element of the array because of the selector $.author here

my $cr = REST::Client::CrossRef->new( mailto => 'd...h', spit_raw_data => 0, add_end_flag => 1, json_path => [ ['$.title'], ['$.author'], ], json_path_callback => { '$.author' => \&unfoldauthors }, );

The unfoldauthors function is a callback pass to the $cr object. The two json_path arg are used to extract data from the whole json hashref. The data extracted with the second argument is given to the callback function.

This is the output (from the complete example) I'm having now, which is ok, but I'm asking if my callback function could have been made simpler:

***The potential of antagonistic moroccan Streptomyces isolates for the biological control of dampingÔÇÉoff disea +se of pea ( Pisum sativum L.) caused by Aphanomyces euteiches Oubaha, Brahim; Laboratory of Biology and Biotechnology of Microorgani +sms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Moro +cco - Laboratory of Interaction PlantÔÇÉMicroorganisms, Department of + Biology University of Fribourg Fribourg Switzerland Nafis, Ahmed; Laboratory of Biology and Biotechnology of Microorganism +s Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocc +o Baz, Mohamed; Laboratory of Biology and Biotechnology of Microorganism +s Faculty of Sciences Semlalia Cadi Ayyad University Marrakech Morocc +o Mauch, Felix; Laboratory of Interaction PlantÔÇÉMicroorganisms, Depart +ment of Biology University of Fribourg Fribourg Switzerland Barakate, Mustapha; Laboratory of Biology and Biotechnology of Microor +ganisms Faculty of Sciences Semlalia Cadi Ayyad University Marrakech +Morocco - Agrobiosciences & Fertilizers Program University Mohamm +ed IV Polytechnic (UM6P) Benguerir Morocco

Replies are listed 'Best First'.
Re^3: JSON::Path and node iteration (updated)
by haukex (Archbishop) on Mar 21, 2019 at 15:51 UTC
    I think that my values are in the first element of the array because of the selector $.author here

    Ah yes, I didn't play around with JSON::Path because I haven't used that yet (and it seemed like it might be a little overkill in this case). Anyway, I think then my unfoldauthors3 should do what you want? (Update: The same $data_ar = $data_ar->[0]; step will probably be necessary with $authors.)

    Update 2: I took a look at the source of REST::Client::CrossRef, and it seems like that's the code that wraps the argument of the callback in an extra arrayref. In unfoldauthors3, you could probably just say @{$authors->[0]} instead of @$authors.

      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 ?

        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' } ] ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1231552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found