#!/bin/sh toEnvVars() { toFlatJson "$1" | jq --raw-output '. | to_entries[] | "\(.key)=\(.value)"' } toFlatJson() { # thank you jeff mercado https://stackoverflow.com/a/42303836/659715 jq --arg delim '__' 'reduce (tostream|select(length==2)) as $i ({}; .[[$i[0][]|tostring]|join($delim)] = $i[1] )' "$1" } # One of the args should be a json file. # If there's only one arg, then that's the json file. if [ -z "$2" ]; then toFlatJson "$1" exit 0 fi # So we've been passed a file. # Assume it's json. Sorry. # Translate the json file to a shell-friendly list of k=v lines [ "$1" = "-e" ] && toEnvVars "$2" # Translate the json file to a flat object of key value pairs [ "$1" = "-j" ] && toFlatJson "$2"