-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jpath, a cut(1) that uses JSONPath to describe fields to output.
- Loading branch information
Showing
7 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/perl | ||
|
||
use App::PipeFilter::JsonPath; | ||
|
||
App::PipeFilter::JsonPath->new_with_options()->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
duckduckgo.json is a sample JSON response from: | ||
curl 'http://api.duckduckgo.com/?q=poe&o=json' > duckduckgo.json | ||
curl 'http://api.duckduckgo.com/?q=poe&o=json' | ||
|
||
reddit.json is sample JSON from http://www.reddit.com/.json | ||
reddit.json is sample JSON from: | ||
curl http://www.reddit.com/.json | ||
|
||
sample.json is just some tabular data I put together for testing. | ||
|
||
delicious-urlinfo.json comes from: | ||
curl -s http://feeds.delicious.com/v2/json/urlinfo/7b6cdc20ad985d4ddc547cd18a9e3998 | ||
|
||
*** Filtering Web Data with JSONPath | ||
|
||
% curl -s 'http://api.duckduckgo.com/?q=poe&o=json' | | ||
jpath -o '$..Topics.*.FirstURL' -o '$..Topics.*.Text' | | ||
jmap -i col0 -o url -i col1 -o title | | ||
grep -i perl | | ||
json2yaml | ||
--- | ||
title: Perl Object Environment, a library for event driven multitasking for the Perl programming language | ||
url: http://duckduckgo.com/Perl_Object_Environment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"hash":"7b6cdc20ad985d4ddc547cd18a9e3998","title":"POE: Perl Object Environment","url":"http:\/\/poe.perl.org\/","total_posts":225,"top_tags":{"perl":204,"programming":104,"poe":100,"framework":73,"development":47,"network":40,"networking":26,"dev":22,"server":16,"software":15}}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package App::PipeFilter::JsonPath; | ||
|
||
use Moose; | ||
extends 'App::PipeFilter::Generic::Json'; | ||
|
||
use JSON::Path; | ||
|
||
has o => ( | ||
is => 'rw', | ||
isa => 'ArrayRef', | ||
default => sub { die "requires one or more -o flag" }, | ||
lazy => 1, | ||
documentation => 'output fields (a subset of the input)', | ||
); | ||
|
||
has _o => ( | ||
is => 'rw', | ||
isa => 'ArrayRef[JSON::Path]', | ||
lazy => 1, | ||
default => sub { | ||
my $self = shift; | ||
return [ map { JSON::Path->new($_) } @{$self->o()} ]; | ||
}, | ||
); | ||
|
||
sub transform { | ||
my $self = shift(); | ||
|
||
my @json_paths = @{$self->_o()}; | ||
|
||
my @output; | ||
|
||
my $c = 0; | ||
foreach my $json_path (@json_paths) { | ||
my $r = 0; | ||
foreach my $json_value ($json_path->values($_[0])) { | ||
$output[$r++]{"col$c"} = $json_value; | ||
} | ||
++$c; | ||
} | ||
|
||
return @output; | ||
} | ||
|
||
1; | ||
|
||
# vim: ts=2 sw=2 expandtab |