Skip to content

Instantly share code, notes, and snippets.

@Jimilian
Last active January 20, 2023 08:11
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jimilian/c3a2d8bb6df1b8ca64d02a10d97f510b to your computer and use it in GitHub Desktop.
Save Jimilian/c3a2d8bb6df1b8ca64d02a10d97f510b 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
@qbatten
Copy link

qbatten commented Feb 21, 2021

Hi @Jimilian, this was really helpful to me! Thanks for writing/publishing it!

I was wondering if you'd be willing to attach a license to it (or if there is one attached that I didn't notice)? I'm hoping to use it in a small bash script for managing AWS credentials. In my draft right now, I reference your Stackoverflow profile as the creator of this piece of code.

Was planning to distribute that under the MIT License, but want to make sure that's alright with you, and if not, I won't!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment