Skip to content

Instantly share code, notes, and snippets.

@sanusart
Created March 4, 2015 08:42
Show Gist options
  • Save sanusart/fd1de52b58bdcb80789a to your computer and use it in GitHub Desktop.
Save sanusart/fd1de52b58bdcb80789a to your computer and use it in GitHub Desktop.
shell get json value by key
#!/usr/bin/env bash
# --- Usage: `./shell_json_parse.sh /path/to/package.json version`
get_value_by_key() {
JSON_FILE=${1}
KEY=${2}
REGEX="(?<=\"${KEY}\":.\")[^\"]*"
JSON_VALUE=$(cat ${JSON_FILE} | grep -Po ${REGEX})
printf "\n-> REGEX: \n${REGEX}\n"
printf "\n-> ${KEY}: \n${JSON_VALUE}\n\n"
}
get_value_by_key ${1} ${2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment