Meet jp

Welcome

Please welcome the latest JSON tool for the command line: jp!

jp (mostly named from the Mojolicious class Mojo::JSON::Pointer that makes jp possible) is a command line tool for quickly extracting data from a JSON object.  How many times do we get a complex JSON object from some command and we just need to extract a specific set of values from it?  Every time!  All the time!  Often, people use basic tools like grep and sed.  Most people reach for jq, but I find the syntax too cumbersome to use just to extract my desired data.  I just want to use JSON Pointers and move on, but, indeed, sometimes JSON Pointers aren't even enough.  jp really shines with some helpful command line arguments, a regular expression JSON Pointer syntax, and, finally, a Perl eval argument for total power.

Get on with it

Let's see jp work.  Each example explainer links to the JSON Object used for the example.

Here's the most basic use.

$ jp /server/0/password < t/json1.json

REDACTED_APIKEYfalse

Now let's use that regex we hyped up a moment ago. This simple regex has too much data to return (3 extracted JSON objects), so let's use a couple JSON Pointers and narrowly extract some data we can look at.

$ jp -r '/server/\d+' /hash/a /password < t/json1.json

1 REDACTED_APIKEYfalse

2 REDACTED1_APIKEYtrue

3 REDACTED_APIKEYtrue

Well, that was fun and all, but let's get on with it. Let's do some case insensitive filtering looking for the default credentials in the list and create a colon-separate output that includes the JSON Pointer from whence they came..

$ jp -ip -d: -E '$_->grep(sub{$_->jp("/isdefault")})' -r '/server/\d' /user /password < t/json1.json

/server/1:0038137:REDACTED1_APIKEYtrue

/server/2:0038137:REDACTED_APIKEYtrue

In this final example showcasing the usefulness of jp we go after some GPS data where not all data is labeled the same. And, on top of that, we sort the coordinates by latitude despite the varying coordinate labels.

$ ./jp -S -U -p -r '/markers/\d' -E '$_->sort(sub{$a->jp("/location/0", "/position/0") <=> $b->jp("/location/0", "/position/0")})' /location /position < t/json2.json

/markers/2 [40.7411595,-74.0097167]

/markers/0 [40.7414728,-74.0055813]

/markers/3 [40.7532822,-74.0010696]

/markers/1 [40.7565323,-73.9904037]

Parting words

Download jp, read the Usage and Examples, and use jp to transform your life forever! Contribute issues and pull requests via Github.

Leave a comment

About Stefan Adams

user-pic