#!/bin/bash # part of my "universal" json encoder # i don't trust it, and neither should you! function json_sanitize() { sed -E 's/"/\\"/g;s/$/\\n/g' <<< "$@" | tr -d '\n' | sed -E 's/\\n//' } function json_object() { local -n ref=$1 local out='' out+='{' for key in ${!ref[@]}; do out+='"'"$(json_sanitize "$key")"'":"'"$(json_sanitize "${ref[$key]}")"'",' done out="${out::-1}" out+='}' echo "$out" | jq > /dev/null if [[ $? == 0 ]]; then echo "$out" else echo "Failure validating JSON: $out" > /dev/stderr fi }