Home

Awesome

jq.node - Become a shell hero, get super-power

Build Status deps Version Docker hub available-for-advisory extra Twitter Follow Get help on Codementor Slack

jq.node is JavaScript and Lodash in your shell (along with the 1.45M npm modules). It's a powerful command-line JSON/string processor. It so easy it feels like cheating your inner-bearded-sysadmin.

Rational

I'm a huge fan of jq but it was so many times inconsistent and irritating. It sometimes felt like JavaScript but it was not. jq.node is what jq should be in my opinion. First version was written in 25 lines of JavaScript code and was already way more powerful than jq, backed from day one by more than 300 helpers from Lodash FP.

Why jq.node? Why not jq?

Why jq? Why not jq.node?

Install (NodeJS)

npm install jq.node -g

Shameless plug

CLI Usage

# the 4 commands below do the same thing
cat users.json | jqn 'filter(has("email")) | groupBy(function(u){return u.email.split("@")[1]}) | csv'
cat users.json | jqn 'filter(has("email")) | groupBy(u => u.email.split("@")[1]) | csv'
cat users.json | jqn 'filter(has("email")) | groupBy(u => get(u.email.split("@"), 1)) | csv'
cat users.json | jqn 'filter(has("email")) | groupBy(flow(get("email"), split("@"), get(1))) | csv'

Note: the pipe | must always be surrounded by space to be understood by jqn as a pipe.

Examples

Be notified when a JSON value changed

while true; do curl -s http://10.10.0.5:9000/api/ce/task?id=AVhoYB1sNTnExzIJOq_k | jqn 'property("task.status"), thru(a => exit(a === "IN_PROGRESS" ? 0 : 1))' || osascript -e 'display notification "Task done"'; sleep 5; done

Open every links from the clipboard

pbpaste | jqn -x -r opn 'split("\n") | forEach(opn)'

Edit a JSON file

This command above rely on tap to add a propety to a package.json file [#89]:

jq.node 'tap(x => x.scripts.build= "sass --load-path=./scss ./scss/style.scss:./css/style.css")' < package.json

API Usage

jq.node exposes a node API for programmatic use. Require the jq function from the main module.

The arguments are jq(input, transformation, options, callback)

const { jq } = require('jq.node')

jq('20111031', 'thru(a => moment.utc(a, "YYYYMMDD"))', { rawInput: true, require: 'moment' }, function (err, result) {
  console.log(result) // "2011-10-31T00:00:00.000Z"
})

or with promises and async/await, via the bluebird module:

const { Promisify } = require('bluebird')
const { jq } = Promisify(require('jq.node'))

const result = await jq('20111031', 'thru(a => moment.utc(a, "YYYYMMDD"))', { rawInput: true, require: 'moment' })
console.log(result) // "2011-10-31T00:00:00.000Z"

Options

CLI ShorthandCLI LonghandAPI OptionTypePurpose
-h--help--Display the help message and exit.
-j--jsonjsonbooleanForce the result to be output as JSON. Without this, jqn outputs strings verbatim and non-strings as JSON.
-x--raw-inputrawInputboolean
-c--colorcolorbooleanColorize JSON (default: detected via chalk/supports-color)
-r--requirerequirearray(string)* Require a NPM module <npm-module-name>.
-v--version--Display the version and exit.

jq.node uses chalk/supports-color to detect whether or not to colorize the output. If the --color flag is provided, it takes precedence. This detection is disabled for programmatic API usage. For programmatic usage, an explicitly passed option takes precedence over the default (false).

Currently supported

Performance

Roadmap

I accept pull-requests!

Changelog

<!-- BACKERS/ --> <h2>Backers</h2> <h3>Maintainers</h3>

No maintainers yet! Will you be the first?

<h3>Sponsors</h3>

No sponsors yet! Will you be the first?

<span class="badge-patreon"><a href="https://patreon.com/fgribreau" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span> <span class="badge-gratipay"><a href="https://gratipay.com/fgribreau" title="Donate weekly to this project using Gratipay"><img src="https://img.shields.io/badge/gratipay-donate-yellow.svg" alt="Gratipay donate button" /></a></span> <span class="badge-flattr"><a href="https://flattr.com/profile/fgribreau" title="Donate to this project using Flattr"><img src="https://img.shields.io/badge/flattr-donate-yellow.svg" alt="Flattr donate button" /></a></span> <span class="badge-paypal"><a href="https://fgribreau.me/paypal" title="Donate to this project using Paypal"><img src="https://img.shields.io/badge/paypal-donate-yellow.svg" alt="PayPal donate button" /></a></span> <span class="badge-bitcoin"><a href="https://www.coinbase.com/fgribreau" title="Donate to this project using Cryptocurrency"><img src="https://img.shields.io/badge/crypto-donate-yellow.svg" alt="crypto donate button" /></a></span>

<h3>Contributors</h3>

These amazing people have contributed code to this project:

<ul><li><a href="http://bit.ly/2c7uFJq">Francois-Guillaume Ribreau</a></li> <li><a href="https://github.com/chocolateboy">chocolateboy</a> — <a href="https://github.com/fgribreau/jq.node/commits?author=chocolateboy" title="View the GitHub contributions of chocolateboy on repository fgribreau/jq.node">view contributions</a></li> <li><a href="https://github.com/bronislav">Anton Ilin</a> — <a href="https://github.com/fgribreau/jq.node/commits?author=bronislav" title="View the GitHub contributions of Anton Ilin on repository fgribreau/jq.node">view contributions</a></li> <li><a href="http://delapouite.com">Bruno Heridet</a></li> <li><a href="https://github.com/thalesmello">Thales Mello</a> — <a href="https://github.com/fgribreau/jq.node/commits?author=thalesmello" title="View the GitHub contributions of Thales Mello on repository fgribreau/jq.node">view contributions</a></li> <li><a href="http://michael.mior.ca">Michael Mior</a></li></ul>

<a href="https://github.com/fgribreau/jq.node/blob/master/CONTRIBUTING.md#files">Discover how you can contribute by heading on over to the <code>CONTRIBUTING.md</code> file.</a>

<!-- /BACKERS --> <!-- LICENSE/ --> <h2>License</h2>

Unless stated otherwise all works are:

<ul><li>Copyright &copy; <a href="http://fgribreau.com/">Francois-Guillaume Ribreau</a></li></ul>

and licensed under:

<ul><li><a href="http://spdx.org/licenses/MIT.html">MIT License</a></li></ul> <!-- /LICENSE -->