Skip to content

Instantly share code, notes, and snippets.

@the-glima
Created January 12, 2021 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-glima/ae408acf415fce312f5a6d9eccf6f1b6 to your computer and use it in GitHub Desktop.
Save the-glima/ae408acf415fce312f5a6d9eccf6f1b6 to your computer and use it in GitHub Desktop.
Get Json Value
#!/usr/bin/env bash
PROP=$1
FILE_PATH=$2
function getJsonValue {
if [[ -z "$PROP" ]]; then
echo "You need to pass the property name"
exit 1
elif [[ -z "$FILE_PATH" ]]; then
echo "You need to pass the file path"
exit 1
fi
readonly local result=$(node -pe "JSON.parse(process.argv[1]).${PROP}" "$(cat ${FILE_PATH})")
if [[ "$result" == "undefined" ]] || [[ "$result" == "null" ]]; then
echo "Property: '${PROP}' not found"
exit 1
fi
echo "${result}"
exit 0
}
getJsonValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment