This is still the header! Main site

Fancifying the Command Line with JSON

2022/04/17

... tl;dr: JSON fits in well with command line piping!

This is technically no longer part of Kev Quirk's #100DaysToOffload challenge. The point, nevertheless, is still to write many things, not to write good ones. Please adjust quality expectations accordingly :)

I recently came across this article on some new-ish command line tools on HN. Which did serve as a reminder for the existence of jq, the JSON query / processing / piping tool.

You can combine this with e.g. jc, which can turn outputs of various command line tools to json, and achieve something like this:

$ jc ls
[{"filename": "battleship.png"}, {"filename": "index.html"}, {"filename": "liblist.png"}]
$ jc ls -l | jq
[
  {
    "filename": "battleship.png",
    "flags": "-rw-r--r--",
    "links": 1,
    "owner": "simon",
    "group": "simon",
    "size": 20947,
    "date": "Jan 24 18:11"
  },
  {
    "filename": "index.html",
    "flags": "-rw-r--r--",
    "links": 1,
    "owner": "simon",
    "group": "simon",
    "size": 15608,
    "date": "Mar 28 21:46"
  },
  {
    "filename": "liblist.png",
    "flags": "-rw-r--r--",
    "links": 1,
    "owner": "simon",
    "group": "simon",
    "size": 16763,
    "date": "Feb 5 13:00"
  }
]
$ jc ls -l | jq '.[] | {.filename, .size}'
{
  "name": "battleship.png",
  "date": "Jan 24 18:11"
}
{
  "name": "index.html",
  "date": "Mar 28 21:46"
}
{
  "name": "liblist.png",
  "date": "Feb 5 13:00"
}
          

Apart from the little chaos with jq and having to expand the JSON array into individual elements it can act on (the ".[]" part), it's a lot easier to handle (and less prone to breaking) than the standard awk + sed + grep combo.

One might even argue that just by doing JSON you can get most of the benefits of Powershell (the "passing objects around instead of just plain text" part), without the downsides (... of your tools not actually just dumping text into pipes, so they're somewhat... harder to implement?). Not having used Powershell a lot though, there might be a lot of things I'm missing here.

Nevertheless... this is yet another instance where, instead of custom borderline-human-readable syntax, we use something more standard, obtaining a lot more flexibility. Just like with programming languages!

... comments welcome, either in email or on the (eventual) Mastodon post on Fosstodon.