update to run on nested object too
This commit is contained in:
parent
796d94f1c6
commit
0801b8e941
|
@ -11,18 +11,39 @@ then
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get the keys from the JSON file
|
# Recursive function to check for keys in nested JSON objects
|
||||||
keys=$(jq -r 'keys[]' "$json_file_path")
|
check_keys() {
|
||||||
|
local keys="$1"
|
||||||
# Loop through the keys
|
local parent_key="$2"
|
||||||
for key in $keys
|
for key in $keys
|
||||||
do
|
do
|
||||||
# Check if the key is present in any of the files in the folder
|
local full_key=""
|
||||||
grep -rqE "'$key'|\"$key\"" "$folder_path"
|
if [[ -z $parent_key ]]; then
|
||||||
if [ $? -ne 0 ]
|
full_key="$key"
|
||||||
then
|
else
|
||||||
echo "$key is not present as a string in any of the files in the folder"
|
full_key="$parent_key.$key"
|
||||||
|
fi
|
||||||
|
local children_keys=$(jq -r --arg key "$key" 'select(.[$key] | type == "object") | .[$key] | keys[]' "$json_file_path")
|
||||||
|
if [ -n "$children_keys" ]; then
|
||||||
|
# check first if the key is not in the ignore list
|
||||||
|
check_keys "$children_keys" "$full_key"
|
||||||
|
else
|
||||||
|
if ! grep -rqE "'$full_key'|\"$full_key\"" "$folder_path"; then
|
||||||
# Remove the key from the JSON file
|
# Remove the key from the JSON file
|
||||||
jq "del(.\"$key\")" "$json_file_path" > "$json_file_path.tmp" && mv "$json_file_path.tmp" "$json_file_path"
|
# echo the command to remove the key from the JSON file
|
||||||
|
jq "del(.$(echo $full_key | sed 's/\./"."/g' | sed 's/^/"/' | sed 's/$/"/'))" "$json_file_path" > "$json_file_path.tmp" && mv "$json_file_path.tmp" "$json_file_path"
|
||||||
|
echo "Removing key \"$full_key\" from the JSON file"
|
||||||
|
else
|
||||||
|
echo "Key \"$full_key\" is being used."
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get the top-level keys from the JSON file
|
||||||
|
keys=$(jq -r 'keys[]' "$json_file_path")
|
||||||
|
|
||||||
|
# Loop through the keys and recursively check for nested keys
|
||||||
|
check_keys "$keys" ""
|
||||||
|
|
||||||
|
echo "Done checking for missing keys."
|
||||||
|
|
Loading…
Reference in a new issue