underscore-cli

Freedom to Deal with JSON on the Command line

What if you wanted to script up some RESTful web service calls that return JSON?

AWK?

sed?

grep?

join?

perl?

regex?

cut?

Gee, I wish there was a tool that would allow me to deal with JSON as JSON and allow me to pipeline the stuff together just like a regular *nix command...

You mean something like:

getServiceInfo "services/third_parties/${tp_id}/service_definitions" | \
    underscore select ":has(.service_id:expr(x=${svc_id})) > .id" | \
    underscore process 'data[0]'

Well, that would be underscore-cli...

Apparently, Javascript guys have known about a package called 'underscore' for a long time that handles JSON.

 

Of course, they wouldn't tell the rest of us about it...

    help [command]      Print more detailed help and examples for a specific command
    type                Print the type of the input data: {object, array, number, 
                        string, boolean, null, undefined}
    print               Output the data without any transformations. Can be used to 
                        pretty-print JSON data.
    pretty              Output the data without any transformations. Can be used to 
                        pretty-print JSON data. (defaults output format to 'pretty')
    run <exp>           Runs arbitrary JS code. Use for CLI Javascripting.
    process <exp>       Run arbitrary JS against the input data.  
                        Expression Args: (data)
    extract <field>     Extract a field from the input data.  Also supports 
                        field1.field2.field3
    map <exp>           Map each value from a list/object through a transformation 
                        expression whose arguments are (value, key, list).'
    reduce <exp>        Boil a list down to a single value by successively combining 
                        each element with a running total.  Expression args: (total, 
                        value, key, list)
    reduceRight <exp>   Right-associative version of reduce. ie, 1 + (2 + (3 + 4)). 
                        Expression args: (total, value, key, list)
    select <jselexp>    Run a 'JSON Selector' query against the input data. See 
                        jsonselect.org.
    find <exp>          Return the first value for which the expression Return a truish 
                        value.  Expression args: (value, key, list)
    filter <exp>        Return an array of all values that make the expression true.  
                        Expression args: (value, key, list)
    reject <exp>        Return an array of all values that make the expression false.  
                        Expression args: (value, key, list)
    flatten             Flattens a nested array (the nesting can be to any depth). 
                        If you pass '--shallow', the array will only be flattened a 
                        single level.
    pluck <key>         Extract a single property from a list of objects
    keys                Retrieve all the names of an object's properties.
    values              Retrieve all the values of an object's properties.
    extend <object>     Override properties in the input data.
    defaults <object>   Fill in missing properties in the input data.
    any <exp>           Return 'true' if any of the values in the input make the 
                        expression true.  Expression args: (value, key, list)
    all <exp>           Return 'true' if all values in the input make the expression true.  
                        Expression args: (value, key, list)
    isObject            Return 'true' if the input data is an object with named 
                        properties
    isArray             Return 'true' if the input data is an array
    isString            Return 'true' if the input data is a string
    isNumber            Return 'true' if the input data is a number
    isBoolean           Return 'true' if the input data is a boolean, ie {true, false}
    isNull              Return 'true' if the input data is the 'null' value
    isUndefined         Return 'true' if the input data is undefined
    template <filename> Process an underscore template and print the results. See 'help 
                        template'

The 'select' command is cool - it's an implementation of JSON selectors (you know, like CSS selectors).

Dude, check it out:

http://jsonselect.org/#overview

That's too awesome. But am I worthy of having a tool of this magnitude on my machine?

I can't speak to your worthiness, but...

Install nodeJs on your machine.

then

sudo npm install -g underscore-cli

Real-world example...

Why not just use Ruby?

I DIDN'T WANT TO

Any more questions?

underscore-cli

By naiveroboticist

underscore-cli

Lightning talk for underscore-cli

  • 1,036