The following is a sample code, remember three points:
- In jq,
[]
it does not necessarily only refer to arrays. When the corresponding element is not an array, but a Json object (or nested object), it[]
can refer to all elements in the object, similar to*
such wildcards - When the field assignment is null, for example: {"MskcPluginName": null}, the value read by using jq is a string with the value "null", instead of reporting an error or returning a null value, which will cause the script to parse this field. Come on, if you don't check, you will treat "null" as a legal value, which is the main reason for using if in the following code to check
- If Json is always generated by Shell scripts, there is no need to use if to check, because then there will not be a Json file whose initial value is all null, but all Json content is written by the command line (as we later as used in the project)
# only when no any field is null or empty, load configs
# this can prevent loading string "null" or "" as value to opts.
if [[ -z $(jq -r '.[] | select(. == null or . == "")' $OSCI_CONF_FILE) ]]; then
MSKC_PLUGIN_NAME=$(echo $mskcPluginConfig | jq -r '.MskcPluginName')
MSKC_PLUGIN_ARN=$(echo $mskcPluginConfig | jq -r '.MskcPluginArn')
checkMskcPluginConfOpts
fi