Skip to content

Instantly share code, notes, and snippets.

@lidopaglia
Created August 30, 2021 13:26
Show Gist options
  • Save lidopaglia/ca641d0ea5f82667cb654b96e417fff7 to your computer and use it in GitHub Desktop.
Save lidopaglia/ca641d0ea5f82667cb654b96e417fff7 to your computer and use it in GitHub Desktop.
returns the value of a json property
#!/bin/bash
function jsonval() {
# extract the value of a json property.
# if avoiding jq as dependency makes sense.
# https://gist.github.com/cjus/1047794
prop="$1";json="$2"
temp=`echo $json \
| sed 's/\\\\\//\//g' | sed 's/[{}]//g' \
| awk -v k="text" '{n=split($0,a,",");
for (i=1; i<=n; i++) print a[i]}' \
| sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' \
| grep -w $prop`
echo ${temp##*|}
}
echo "$(jsonval 'some_key' $some_json)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment