Skip to content

Instantly share code, notes, and snippets.

@rastislavcore
Last active January 27, 2023 11:49
Show Gist options
  • Save rastislavcore/940ffc99d2ddb8ff555e059a3ed59e1f to your computer and use it in GitHub Desktop.
Save rastislavcore/940ffc99d2ddb8ff555e059a3ed59e1f to your computer and use it in GitHub Desktop.
Parse Json with Bash (very simple)
parse_json()
{
echo $1 | \
sed -e 's/[{}]/''/g' | \
sed -e 's/", "/'\",\"'/g' | \
sed -e 's/" ,"/'\",\"'/g' | \
sed -e 's/" , "/'\",\"'/g' | \
sed -e 's/","/'\"---SEPERATOR---\"'/g' | \
awk -F=':' -v RS='---SEPERATOR---' "\$1~/\"$2\"/ {print}" | \
sed -e "s/\"$2\"://" | \
tr -d "\n\t" | \
sed -e 's/\\"/"/g' | \
sed -e 's/\\\\/\\/g' | \
sed -e 's/^[ \t]*//g' | \
sed -e 's/^"//' -e 's/"$//'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment