Friday, October 18, 2013

bash regex to get json value

i needed a bash regex to get the value for a given key in a json file. below is my solution.

json.sh

getJsonValue()
{
    grep -Po '"'"$2"'"\s*:\s*"\K([^"]*)' $1
}

# example usage
# getJsonValue "json_file" "json_key"

 

1 comment:

Anonymous said...

thx