Jo - JSON output from a shell
github.com
github.com
$ python -c 'import json, sys;\
kv = sys.argv[1::];\
v = [int(x) if x.isdigit() else eval(x.title()) if x in ["false", "true"]\
else x for x in kv[1::2]];\
print json.dumps(dict(zip(kv[::2], v)), ensure_ascii=False, indent=2)
' key1 value1 key2 'string value 2' number 5589 boolean1 true boolean2 false
which produces the output {
"key2": "string value 2",
"key1": "value1",
"number": 5589,
"boolean2": false,
"boolean1": true
}
I, for one, am excited to hear that native JSON (and XML, and HTML) output is coming to FreeBSD's userland courtesy of libxo (see https://wiki.freebsd.org/LibXo). I hope GNU Core Utilities eventually go the same way or a full-featured alternative appears for Linux that does. sudo bash -c "cd /usr/local && wget -O - https://github.com/micha/json-table/releases/download/2.0.0/jt-2.0.0.tar.gz | tar xzvf -"That's not curl | bash.
That's wget | tar.
However... I do wish the name was `json` not `jo`. In my case more for personal reasons regarding painful memories attached to the name 'Jo', but I also think the tool creates JSON, so why not call it `json` or `jp` short for 'json print' ... but the matter appears settled. Oh well.
echo 'alias json=jo' >> ~/.bashrc
Or even better sudo ln -s $(which jo) /usr/local/bin/jsonAll these smart small tools are amazing, but they suppose the user has only one computer and does everything on it. so he can just install once and that's it.
I normally use 4 different desktops and 2 VPSs weekly. Although I tend to do most of the work in one of the VPSs, the more I customize it with tools like this, the more all the other computers will miss things.
What is the solution?
$ false; echo success@$? | jo
{"success":true}
i.e. jo's translation of integers to booleans does not match that of a typical shell.Edit: or you're saying jo should always consider 0 true and 1 false to match shell return code conventions?
It was merely an observation. Of course, one can simply negate the value before passing it to jo.
jo looked as though it was for use in the shell though, so I was curious as to the design decision here.
I think it makes sense how it is, since if I typed "success@1" I wouldn't expect that to output "{success: false}" but I definitely appreciate your point about a case like "success@$?".
> Of course, one can simply negate the value before passing it to jo.
What's the shortest way to do that? This?
echo success@$(test $? -ne 0; echo $?) $ false; echo success@$((! $?))
success@0Other than that it looks pretty handy.
ls | jo -a -B