Professional Documents
Culture Documents
A Shell Command To Create JSON Jo
A Shell Command To Create JSON Jo
com/p/g/ABV3AZ
I got tired of attempting to get shell scripts to produce valid JSON. You’ve likely seen something like this before:
echo '{"name":"Jane"}'
It gets merrier if an element contains an environment variable: open double, close single, add variable, open
single, blergh.
A here! script will do it as will a a printf(1), but neither much improve legibility, and if strings contain quotes, it
becomes almost impossible to make a script produce JSON.
Enter jo:
$ jo name=Jane
{"name":"Jane"}
Inspired by a comment on HN, I added another hack: if a key’s value begins with an opening brace ( { ) or a
bracket ( [ ]) we attempt to decode JSON from it; this allows jo to add objects or arrays (use -a !) to itself.
Watch:
Why did I do this? I need lots of JSON for testing OwnTracks, and this just looks better in scripts.
$ jo _type=location \
cog=$((RANDOM % 360)) \
t=u \
lat=48.85833 \
lon=2.29513 \
acc=5 \
tid=JJ \
tst=$(date +%s) | mosquitto_pub -t owntracks/jjolie/test -l
A suggestion by Andrew Bibby brought along the possibility of obtaining JSON element values from files, so I
added @file to read a file (sans traling newline or carriage return) into a value as well as something which I
use a lot, convert binary files to base64, using the %file syntax:
Updates:
Caius Durling provided a Homebrew tap for jo, but jo is in Homebrew Formulae now, so brew
install jo .
jo has a logo which it created itself; colors inspired by something Frederic Cambus said :-)
Shawn Webb added jo to HardenedBSD’s ports.
I’ve converted jo to use autotools; releases will be published here.
Alessio Sergi has added jo to voidlinux; install with xbps-install -Su jo
I’ve built a Win32 binary which you can get from releases/.
Lance Chen has an ArchLinux port of jo.
Vincent Bernat has submitted jo to Debian.