I review tools by feeding them the inputs the README didn’t plan for. jo is a small C program by Jan-Piet Mens that builds JSON from shell arguments, and it exists because writing JSON by hand in bash goes something like echo "{\"name\":\"$name\"}" right up until somebody’s name has a quote in it. jo takes key=value words and prints a valid object. It does the quoting and the escaping for you, and it guesses the types too. That last part is a problem.

The guessing is the feature. It’s also the source of every result below, plus a few the guessing didn’t cause. It took three absurd scenarios to get to the real bug, which is the house average. The first one found something too.

The verdict, up front: use it, with conditions. For JSON you write by hand at the prompt, it’s the fastest correct tool in the drawer. It survives a normal Tuesday. It doesn’t survive a bad Tuesday as-is: the first time a variable holds a zip code, a version string, or a comment that starts with @, the output changes type or picks up a file’s contents. And on the Tuesday where the intern has sudo, the intern pipes in a Latin-1 CSV and jo dumps core. I found a spelling that fixes almost all of it, and I measured how close “almost” is.

Everything below ran on Ubuntu 26.04.1 with the distro package (sudo apt-get install jo, version 1.9-2). Version 1.9 is also the newest upstream release, tagged in November 2022. It’s in Homebrew too.

$ jo -v
jo 1.9

The good part, which is real

$ jo name=ed age=42 admin=true nothing=null
{"name":"ed","age":42,"admin":true,"nothing":null}
$ jo -p -d . user.name=ed user.tags[]=qa user.tags[]=nitpick
{
   "user": {
      "name": "ed",
      "tags": [
         "qa",
         "nitpick"
      ]
   }
}
$ jo -a 1 two true null
[1,"two",true,null]

Numbers come out as numbers, true comes out as a boolean, -d . builds nested objects from dotted keys, key[]= appends to an array, and -a makes an array. A name with a newline, an emoji, a quote and ;DROP TABLE in it came out as one correctly escaped string every time I tried it. Nothing about the quoting broke, and I tried hard to break it. That’s grudging respect, and I don’t hand it out often.

Surprise one: it guesses your types, and your zip code loses

The guessing that makes age=42 a number also applies to every value that merely looks like a number:

$ jo zip=02134 id=007 ver=1.10 ver2=1.2.3 big=9007199254740993 hex=0x1F
{"zip":2134,"id":7,"ver":1.1,"ver2":"1.2.3","big":9007199254740992,"hex":31}

Count the casualties. Boston’s 02134 became 2134, a valid zip code for nowhere in particular. Agent 007 became 7. Version 1.10 became 1.1, which is a different, older version, while 1.2.3 stayed a string because it can’t be parsed as a number. 2^53 + 1 came back one smaller, because it went through a double. 0x1F became 31, which was nice of it, I suppose, but I didn’t ask. The failure this prevents: the API on the other end gets "zip": 2134, the shipping label prints a four-digit zip, and nobody looks at the JSON because it’s valid.

The fix is the -s (string) coercion, and it has a spelling trap. jo -s zip=… fails with invalid option -- 's', because these are per-word flags that only work after --. “Per-word” means exactly one word:

$ jo -- -s zip=02134 id=007 ver=1.10 port=8080
{"zip":"02134","id":7,"ver":1.1,"port":8080}

The zip was saved. The id and the version, standing right next to it, were not. You need -s in front of every word you want kept as a string. The man page does say this, in a table I read after the fact. It also documents -n (number) and -b (boolean) coercion. -n on a non-number gives you the length of the string, which is a creative interpretation of “number”.

Surprise two: a value that starts with @ is a file read

This is the one to read twice. jo treats a value that starts with @, % or : as a filename and substitutes the file’s contents: plain, base64-encoded, or parsed as JSON. That’s documented, and it’s useful when you build JSON from files on purpose. Now picture a variable holding whatever a user typed into a comment box. I put the word hunter2 in a file called secret.txt:

$ jo note="@secret.txt"
{"note":"hunter2"}
$ jo -- -s note="@secret.txt"
{"note":"hunter2"}
$ jo note="%secret.txt"
{"note":"aHVudGVyMgo="}
$ echo hunter2 | jo note=@-
{"note":"hunter2"}

-s does not save you here. The file read happens before the type coercion, so “force this to be a string” just makes sure the stolen file arrives as a string. @- reads stdin, which is how my first 10,000-run loop hung until I killed it: one random value came out as @-, and that jo sat there waiting for input the loop never sent. The failure this prevents: a webhook script does jo text="$COMMENT", someone comments @/home/deploy/.env, and your secrets get posted to the chat channel. It also prevents the quieter version, where an honest comment like @channel lunch? makes the job exit 1 with Cannot open channel lunch? for reading.

The documented escape is a leading backslash. It’s also safe to add to every value, because jo drops one leading backslash even when nothing follows it that needs escaping:

$ jo -- -s note="\@secret.txt"
{"note":"@secret.txt"}

The same rule eats a backslash that was really part of your data, so a Windows path that starts with \\server loses one. Always prefixing your own backslash handles that case too. So the safe spelling for untrusted input is jo -- -s key="\\$var", on every word. It’s not pretty. It is correct (almost), and I measured the “almost” below.

Surprise three: the scripts never find out

Two quieter results, both about exit codes, both from the “a cron job ran this and nobody watched” school of failure.

$ jo a=1 a=2
{"a":1,"a":2}
$ jo a=1 a=2 | jq -c .
{"a":2}
$ jo k=v broken; echo "exit=$?"
Argument `broken' is neither k=v nor k@v
{"k":"v"}
exit=0
$ jo; echo "exit=$?"
{}
exit=0

Duplicate keys go out as-is. Which one the reader keeps is up to the reader: jq keeps the last one, and other parsers aren’t required to agree. -D deduplicates if you remember it. A word with no = gets a warning on stderr, then dropped, and the exit code is 0. The failure this prevents: jo name=$NAME with an empty, unquoted NAME turns into the word name=, which is fine, but jo $FIELDS with a field that lost its = turns into an object with a missing key and a green build. Check stderr or check the keys. The exit code won’t tell you.

Surprise four: one bad byte, and it dumps core

The third absurd scenario is the one I was waiting for. I gave it café encoded in Latin-1, which is a byte sequence that isn’t valid UTF-8. That’s what you get from older CSV exports, some Windows tools, and a fair share of filenames that have been around since 2004:

$ jo bad="$(printf 'caf\xe9')"; echo "exit=$?"
jo: json.c:1209: emit_string: Assertion `utf8_validate(str)' failed.
bash: line 1: 63233 Aborted                    (core dumped) jo bad="$(printf 'caf\xe9')"
exit=134

It doesn’t replace the byte, escape it, or print an error. It fails an assert() and aborts, with exit 134 and no stdout. In fairness, exit 134 at least makes a pipeline fail, which is more than exit=0 managed in surprise three. The assertion has come up upstream before: issues #53, #184 and #191 are all closed, and #191 ended in a fix for long stdin lines. 1.9 is still the newest release, and on 1.9 one invalid byte in an argument still aborts. The failure this prevents: a nightly export loop that dies on the first customer named Zoë whose name was saved by a 2004 system. Clean the bytes first, with iconv -f latin1 -t utf-8 if you know the encoding.

The 10,000-run table

To measure how much escaping is enough, I generated 10,000 random values (seed 2038, naturally). Each one was 0–12 characters drawn from letters, digits, quotes, backslashes, {}[]:,=@%-+., tab, newline, an emoji, é, U+2028, $(id) and ;DROP TABLE. I passed each one as v=<value>, parsed the output with Python’s json module, and checked whether v came back byte-for-byte. Stdin was closed this time, after the @- incident. Same 10,000 values for all three spellings:

Spelling Round-tripped exactly Type-guessed Lost a leading \ Read a file / errored
jo v="$x" 7,986 ❌ 837 266 911
jo -- -s v="$x" 8,823 ❌ 0 266 911
jo -- -s v="\\$x" 9,958 ❌ 0 0 42

Read the columns plainly. The plain spelling lost about one value in five: 734 empty strings became null (documented behavior), 100 became integers, two became floats, and one turned into an object because it happened to be valid JSON. 911 values read a file or errored: 879 started with @, % or : and pointed at files that didn’t exist, 30 contained := (more on that below), and two were @-, which read the empty stdin and came back as "". In a real directory, some of them would have existed, and that’s the scary row. -s fixed the type guesses and nothing else.

The backslash spelling got to 9,958, which is the best result and still not 10,000. All 42 failures contain := somewhere in the value, and jo refuses them with Cannot open … for reading and exit 1, backslash or not:

$ jo -- -s note='a:=b'; echo "exit=$?"
a:=b: No such file or directory
jo: Cannot open a:=b for reading
exit=1

:= is jo’s syntax for “read this key’s value from a JSON file”. I didn’t find an escape that gets the literal text a:=b through as a value. At least it fails loudly with exit 1, which is the one failure in this review that a script will notice on its own. I’d take loud over silent any day.

What I’d tell you at standup

  • Typing at a prompt, or values you control: use jo. It’s faster than writing JSON by hand and it never gets the quoting wrong.
  • Values from a variable: write jo -- -s key="\\$var" for every word, or the zip codes, version strings and @-comments will bite you. If a value can contain :=, this spelling still fails, but it fails loudly.
  • Bytes from anywhere old: run them through iconv first, or one accented name aborts the whole loop.
  • Exit codes: a dropped word exits 0, so check stderr or check the keys on the other end.
  • Untrusted input, seriously: jq -n --arg v "$var" '{v: $v}' treats the value as a plain string: no type guessing, no @/%/: prefixes, no :=. I ran the same 10,000 values through it and got 10,000 out of 10,000 back. It’s more typing. It also never reads a file because of what a user typed.

jo is a good little tool with a guessing engine that assumes you mean well. Your users won’t always.