import json
import os
import sys
def display_json():
directory = os.getcwd()
# Get a list of all files in the directory
files = os.listdir(directory)
# Filter out JSON files
json_files = [file for file in files if file.endswith('.json')]
for i in json_files:
print(i)
def display_dictionaries():
count = 1
for i in dictionary_list:
print("Dictionary ",count, ">>")
print(i)
count += 1
dictionary_list = []
while True:
print("1. View list of JSON files in the folder")
print("2. View list of dictionaries")
print("3. Convert JSON to dictionary")
print("4. Convert Dictionary to JSON")
user_input = int(input(">> "))
if user_input == 1:
display_json()
elif user_input == 2:
display_dictionaries()
elif user_input == 3:
display_json()
file_name = input("Enter JSON file name: ")
with open(file_name, "r") as json_file:
data = json.load(json_file)
dictionary_list.append(data)
print(data) # data --> dict
elif user_input == 4:
display_dictionaries()
dict_name = int(input("Enter Dictionary number which needs to be saved: "))
json_data = json.dumps(dictionary_list[dict_name-1])
file_name_to_save = input("Enter the file name to save the JSON data: ")
# Save the JSON data to a new file
with open(file_name_to_save, "w") as json_file:
json_file.write(json_data)
print(f"JSON data saved to {file_name_to_save}")
else:
print("Invalid input, Enter again!")
while True:
option = input("Do you want to continue? (y/n): ")
option = option.lower()
if option == "y":
break
elif option == "n":
sys.exit()
else:
print("Invalid option, Enter again!")