thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

jo: create JSON from shell arguments

β€’ 208 words β€’ 1 min

Today I learned: jo creates JSON from shell arguments. jq queries and transforms JSON. They work well together, complementing each other.

Both can create objects. jo:

shell
% repo=$(basename "$PWD")
% branch=$(git branch --show-current)
% jo repo="$repo" branch="$branch"
{"repo":"perrotta.dev","branch":"master"}

The equivalent with jq needs -n (no input) and --arg for each string. -c keeps the output compact:

shell
% jq -cn --arg repo "$repo" --arg branch "$branch" '{repo: $repo, branch: $branch}'
{"repo":"perrotta.dev","branch":"master"}

My take: jo is more ergonomic to create JSON than jq.

jo -a creates an array. Command substitution lets us nest it in an object:

shell
% jo -a bloggify dev
["bloggify","dev"]
% jo repo="$repo" tags="$(jo -a bloggify dev)"
{"repo":"perrotta.dev","tags":["bloggify","dev"]}

My take: not sure whether this is super useful.

Shell quotes prevent word splitting. They do not force JSON strings:

shell
% version=$(jo -v | cut -d ' ' -f 2)
% jo version="$version"
{"version":1.9}
% jo -- -s version="$version"
{"version":"1.9"}

jo detects numbers and booleans. Use -- -s to force the next value to a string. By contrast, jq --arg always creates a string.

For querying or changing the result, pass it to jq:

shell
% jo repo="$repo" branch="$branch" | jq -r '.branch'
master

jo for short JSON creation commands; jq for filters and transformations. ∎

πŸ€– This post was drafted with an AI agent through /bloggify, then reviewed and edited by me. More agent-assisted posts.

ESC