Created
January 12, 2021 11:09
-
-
Save the-glima/ae408acf415fce312f5a6d9eccf6f1b6 to your computer and use it in GitHub Desktop.
Get Json Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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