Skip to content

Instantly share code, notes, and snippets.

@skwid138
Created February 24, 2025 19:23
Show Gist options
  • Save skwid138/f08032e05412c93adef05a7bcaa2e7f5 to your computer and use it in GitHub Desktop.
Save skwid138/f08032e05412c93adef05a7bcaa2e7f5 to your computer and use it in GitHub Desktop.
This script decodes base64-encoded strings to JSON and pretty-prints the output, supporting both direct command-line input and standard macOS base64 options.
#!/bin/bash
# Script to decode base64 and pretty-print JSON while supporting standard base64 options
# Check for jq dependency
check_dependencies() {
if ! command -v jq &>/dev/null; then
echo "Error: This script requires 'jq' but it's not installed." >&2
echo "To install jq on macOS, run: brew install jq" >&2
echo "For other platforms, visit: https://stedolan.github.io/jq/download/" >&2
exit 1
fi
}
# Display usage information
show_usage() {
cat <<EOF
NAME
base64_2_json - Decode base64 to JSON and pretty-print the result
SYNOPSIS
base64_2_json [OPTIONS] [BASE64_STRING]
DESCRIPTION
This script decodes a base64 encoded string and pretty-prints it if the
result is valid JSON. It supports the standard macOS base64 options plus
a simplified mode where the base64 string is passed as an argument.
OPTIONS
-h, --help Display this help message
-D, --decode Decode mode (automatically enabled)
-b, --break Break encoded string into num character lines
-i, --input Input file (default: "-" for stdin)
-o, --output Output file (default: "-" for stdout)
EXAMPLES
# Decode a base64 string passed as argument
base64_2_json eyAiZGF0YSI6ICJoZWxsbyB3b3JsZCIgfQ==
# Output: { "data": "hello world" }
# Decode from a file
base64_2_json -i encoded.txt
# Pipe input
echo "eyAiZGF0YSI6ICJoZWxsbyB3b3JsZCIgfQ==" | base64_2_json
# Save output to a file
base64_2_json encoded_string -o decoded.json
EOF
}
# Process a base64 string and attempt to pretty-print as JSON
process_json() {
local decoded
# Decode the base64 input
decoded=$(cat)
# Check if the decoded content is valid JSON
if echo "$decoded" | jq '.' &>/dev/null; then
# If valid JSON, pretty-print it
echo "$decoded" | jq '.'
else
# If not valid JSON, just output the decoded text
echo "$decoded"
echo "Warning: Decoded content is not valid JSON" >&2
fi
}
# Main function
main() {
# Check for dependencies first
check_dependencies
local b64_args=""
local input_source=""
local output_dest=""
local direct_input=""
# No arguments provided, show usage
if [ $# -eq 0 ]; then
show_usage
exit 0
fi
# Process arguments
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
show_usage
exit 0
;;
-D | --decode)
# Decode is already our default behavior
shift
;;
-b | --break)
b64_args="$b64_args $1 $2"
shift 2
;;
-i | --input)
if [ -n "$direct_input" ]; then
echo "Error: Cannot specify both direct input and -i option" >&2
exit 1
fi
input_source="$2"
shift 2
;;
-o | --output)
output_dest="$2"
shift 2
;;
-*)
echo "Unknown option: $1" >&2
show_usage
exit 1
;;
*)
# If no -i was specified, treat this as the direct input
if [ -z "$input_source" ]; then
direct_input="$1"
else
echo "Error: Unexpected argument: $1" >&2
exit 1
fi
shift
;;
esac
done
# Set up the input source
if [ -n "$direct_input" ]; then
# Input from argument
decode_cmd="echo \"$direct_input\" | base64 -D $b64_args"
elif [ -n "$input_source" ]; then
# Input from file
decode_cmd="base64 -D $b64_args -i \"$input_source\""
else
# Input from stdin
decode_cmd="base64 -D $b64_args"
fi
# Set up the output destination
if [ -n "$output_dest" ]; then
# Output to file
eval "$decode_cmd" | process_json >"$output_dest"
else
# Output to stdout
eval "$decode_cmd" | process_json
fi
}
# Run the script
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment