Skip to content

Commit

Permalink
GUI update and README updated to reflect changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepypeanut committed Jan 30, 2024
1 parent 11b4606 commit e399142
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
75 changes: 55 additions & 20 deletions JSONcombiner.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
#JSONCombiner.py
#if running twice in same day, make sure to

import os
import json
from datetime import datetime
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
from ttkthemes import ThemedStyle

class JSONCombinerGUI:
def __init__(self, master):
self.master = master
self.master.title("JSON Combiner")
self.master.geometry("500x200")

self.style = ThemedStyle(self.master)
self.style.set_theme("plastik") # You can choose different themes

self.folder_path = tk.StringVar()
self.output_file = tk.StringVar()

ttk.Label(master, text="Select Folder:").grid(row=0, column=0, pady=10, padx=10, sticky="w")
ttk.Entry(master, textvariable=self.folder_path, width=30).grid(row=0, column=1, pady=10, padx=10, sticky="w")
ttk.Button(master, text="Browse", command=self.browse_folder).grid(row=0, column=2, pady=10, padx=10, sticky="w")

ttk.Label(master, text="Output File:").grid(row=1, column=0, pady=10, padx=10, sticky="w")
ttk.Entry(master, textvariable=self.output_file, width=30).grid(row=1, column=1, pady=10, padx=10, sticky="w")
ttk.Button(master, text="Save As", command=self.save_as).grid(row=1, column=2, pady=10, padx=10, sticky="w")

ttk.Button(master, text="Combine JSON", command=self.combine_json).grid(row=2, column=1, pady=20, padx=10, sticky="ew")

def browse_folder(self):
folder_selected = filedialog.askdirectory()
self.folder_path.set(folder_selected)

def save_as(self):
file_selected = filedialog.asksaveasfilename(defaultextension=".json", filetypes=[("JSON files", "*.json")])
self.output_file.set(file_selected)

def combine_json(self):
folder_path = self.folder_path.get()

if not os.path.exists(folder_path):
tk.messagebox.showerror("Error", "Selected folder does not exist.")
return

folder_path = "Files"
files = os.listdir(folder_path)
python_objects = []

# List all files in the folder
files = os.listdir("Files")
for file_name in files:
if os.path.isfile(os.path.join(folder_path, file_name)) and file_name.endswith('.json'):
with open(os.path.join(folder_path, file_name), 'r') as file:
python_objects.append(json.load(file))

python_objects = []
output_file = self.output_file.get() or f"combined_{datetime.now().strftime('%Y-%m-%d')}.json"

with open(output_file, "w") as f:
json.dump(python_objects, f, indent=4)

# Loop through each file
for file_name in files:
# Check if it's a file (not a subdirectory)
if os.path.isfile(os.path.join(folder_path, file_name)) and file_name.endswith('.json'):
# Process the file as needed
print(f"Processing file: {file_name}")

# Example: Read the content of the file
with open(os.path.join(folder_path, file_name), 'r') as file:
python_objects.append(json.load(file))
tk.messagebox.showinfo("Success", "JSON files combined successfully.")

# Dump all the Python objects into a single JSON file.
with open(os.path.join(f"combined_{datetime.now().strftime('%Y-%m-%d')}.json"), "w") as f:
json.dump(python_objects, f, indent=4)
if __name__ == "__main__":
root = tk.Tk()
app = JSONCombinerGUI(root)
root.mainloop()
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Combine Multiple JSON Files of the same structure

## Instructions:
Put whatever JSON files that need to be combined into the **Files** folder, and run the script.
Put whatever JSON files that need to be appended to one another into a folder of their own.

Run the JSONCombiner.py script to pull up the python GUI. Optionally, convert the script to an .exe file for long-term use.

![Python GUI with selection areas for JSON files and Final File nmae](image.png)

From the Python GUI, select the location of the folder contianing all files and set a name for the combination file, which outputs into the JSONCombiner Folder.

The Script should output a file named "combined_[date].json"
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e399142

Please sign in to comment.