jo

jo is a command-line json builder.

I often see coworkers in chats passing around request snippets or API call examples with the json baked in as escaped strings.

It usually looks something like this:

curl \
  -X POST \
  -H "Content-Type: application/json" \
  -d "{\"foo\": {\"bar\": \"baz\"}}" \
  https://httpbin.org/post

Swapping the outer double quotes for single quotes isn’t the fix here. Snippets like this are generated by an http client during debugging. The output gets copied and pasted straight into chat.

It looks awful and is unreadable. Editing this kind of json correctly on the first try is hard (read: impossible).

To generate and edit json from the command line with less pain, use jo:

jo foo=$(jo bar=baz) | jq

The tool lets you build json on the fly and pipe it through to the next stage:

jo foo=$(jo bar=baz) |
  curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d @- \
    https://httpbin.org/post

jo works well alongside grpcurl:

jo f_string=foo f_int64=42 |
  grpcurl \
    -d @ \
    grpcb.in:9001 \
    grpcbin.GRPCBin.DummyUnary |
  jq

Examples

# int
jo foo=1 bar=2 | jq

# string
jo foo=faz bar=baz | jq

# nested
jo this=$(jo is=$(jo nested=json)) | jq

# bool
jo ok=true err=false | jq

# array
jo -p -a foo bar baz | jq

References