sample.py
import json
import copy
def read_json(file_path):
with open(file_path, "r") as file:
data = json.load(file)
return data
def write_json(file_path, data):
with open(file_path, "w") as file:
json.dump(data, file, indent=4)
def edit_json(food_dict):
"""nameの値を小文字に変換"""
edited_data = copy.deepcopy(food_dict)
for data in edited_data.values():
data["name"] = data["name"].lower()
return edited_data
def main():
file_path = "sample.json"
food_dict = read_json(file_path)
edited_data = edit_json(food_dict)
write_json(file_path, edited_data)
if __name__ == "__main__":
main()
sample.json
{
"food1": {
"name": "APPLE",
"price": 150
},
"food2": {
"name": "BANANA",
"price": 100
}
}