Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save virtadpt/31f777db05748ba8e551dcb0a82f295f to your computer and use it in GitHub Desktop.
Save virtadpt/31f777db05748ba8e551dcb0a82f295f to your computer and use it in GitHub Desktop.
Returns json created from pipe
#!/bin/bash
# Example:
# > echo "key1 value1 key2 value2" | ./key_value_pipe_to_json.sh
# {'key1': value1, 'key2': value2}
arr=();
while read x y;
do
arr=("${arr[@]}" $x $y)
done
vars=(${arr[@]})
len=${#arr[@]}
printf "{"
for (( i=0; i<len; i+=2 ))
do
printf "\"${vars[i]}\": ${vars[i+1]}"
if [ $i -lt $((len-2)) ] ; then
printf ", "
fi
done
printf "}"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment