Skip to content

Instantly share code, notes, and snippets.

@nilium
Created December 20, 2018 23:57
Show Gist options
  • Save nilium/54475b4a65da9baa6bbb062105f626a2 to your computer and use it in GitHub Desktop.
Save nilium/54475b4a65da9baa6bbb062105f626a2 to your computer and use it in GitHub Desktop.
Short bash script to create a JSON object from a sequence of key-value arguments
#!/usr/bin/env bash
case "$1" in
-h|--help)
echo 'Usage: jsonkv [-h|--help] [--] KEY VALUE...' 1>&2
exit 2
;;
--)
shift
;;
esac
while [ $# -gt 0 ]; do
jq -n --arg k "$1" --arg v "$2" '{ ($k): (try ($v | fromjson) catch $v) }'
shift
shift
done | jq -s add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment