-
Notifications
You must be signed in to change notification settings - Fork 1
/
modify_json.sh
37 lines (26 loc) · 1006 Bytes
/
modify_json.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
# Specify the path to the JSON file
json_file="users.json"
# Check if the specified JSON file exists
if [ ! -e "$json_file" ]; then
echo "Error: The specified JSON file '$json_file' does not exist."
exit 1
fi
# Extract and output only the keys from the JSON file
keys=$(cat "$json_file" | grep -o '"[^"]*":' | sed 's/"//g; s/://')
# Remove duplicate keys and exclude "mode"
unique_keys=$(echo "$keys" | grep -v "mode" | sort -u)
# Remove trailing spaces and closing parentheses from each key
cleaned_keys=$(echo "$unique_keys" | sed 's/[ \t)]*$//')
# Create a JSON file with each key and value "mode":"Simple"
output_file="users.json"
echo "{" > "$output_file"
# Loop through each cleaned key
echo "$cleaned_keys" | while IFS= read -r key; do
echo " \"$key\":{\"mode\":\"Simple\"}," >> "$output_file"
done
# Remove the trailing comma from the last line
sed -i '$s/,$//' "$output_file"
echo "}" >> "$output_file"
# Print the cleaned keys
echo "Created JSON file: $output_file"