kivilaid commited on
Commit
86cc0b2
·
verified ·
1 Parent(s): 33541c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -24,7 +24,7 @@ def extract_and_combine_zip(zip_file):
24
  data = json.loads(decoded_content)
25
  combined_data = flatten_json(data, combined_data)
26
  except (UnicodeDecodeError, json.JSONDecodeError) as e:
27
- st.warning(f"Warning: Could not decode {json_file}. Error: {str(e)}")
28
  return combined_data
29
 
30
  # Improved function to flatten and merge JSON data
@@ -57,26 +57,36 @@ def to_excel(df):
57
  return processed_data
58
 
59
  # Streamlit app setup
60
- st.title("ZIP JSON Extractor & Flattener")
61
 
62
- # File uploader widget
63
- uploaded_zip = st.file_uploader("Upload ZIP file containing JSON files:", type="zip")
64
 
65
- if uploaded_zip:
66
- # Combine and flatten JSON data
67
- flattened_json = extract_and_combine_zip(uploaded_zip)
 
 
 
 
 
 
68
 
69
  # Create a DataFrame from the flattened JSON data
70
- df = pd.DataFrame([flattened_json])
71
 
72
  # Convert all object columns to string to avoid Arrow conversion issues
73
  for col in df.select_dtypes(include=['object']).columns:
74
  df[col] = df[col].astype(str)
75
 
76
  # Create a downloadable JSON
77
- flattened_json_str = json.dumps(flattened_json, indent=4)
78
  json_bytes = flattened_json_str.encode()
79
 
 
 
 
 
80
  # Create columns for download buttons
81
  col1, col2 = st.columns(2)
82
 
@@ -100,4 +110,4 @@ if uploaded_zip:
100
  )
101
 
102
  # Add a success message
103
- st.success("JSON data has been successfully processed and flattened into a single object. You can now download the flattened JSON file or the Excel file.")
 
24
  data = json.loads(decoded_content)
25
  combined_data = flatten_json(data, combined_data)
26
  except (UnicodeDecodeError, json.JSONDecodeError) as e:
27
+ st.warning(f"Warning: Could not decode {json_file} in {zip_file.name}. Error: {str(e)}")
28
  return combined_data
29
 
30
  # Improved function to flatten and merge JSON data
 
57
  return processed_data
58
 
59
  # Streamlit app setup
60
+ st.title("Multi-ZIP JSON Extractor & Flattener")
61
 
62
+ # File uploader widget for multiple files
63
+ uploaded_zips = st.file_uploader("Upload ZIP files containing JSON files:", type="zip", accept_multiple_files=True)
64
 
65
+ if uploaded_zips:
66
+ # Process all uploaded ZIP files
67
+ all_flattened_json = {}
68
+ total_files_processed = 0
69
+
70
+ for zip_file in uploaded_zips:
71
+ flattened_json = extract_and_combine_zip(zip_file)
72
+ all_flattened_json.update(flattened_json)
73
+ total_files_processed += 1
74
 
75
  # Create a DataFrame from the flattened JSON data
76
+ df = pd.DataFrame([all_flattened_json])
77
 
78
  # Convert all object columns to string to avoid Arrow conversion issues
79
  for col in df.select_dtypes(include=['object']).columns:
80
  df[col] = df[col].astype(str)
81
 
82
  # Create a downloadable JSON
83
+ flattened_json_str = json.dumps(all_flattened_json, indent=4)
84
  json_bytes = flattened_json_str.encode()
85
 
86
+ # Display processing summary
87
+ st.write(f"Processed {total_files_processed} ZIP file(s)")
88
+ st.write(f"Total JSON objects flattened: {len(all_flattened_json)}")
89
+
90
  # Create columns for download buttons
91
  col1, col2 = st.columns(2)
92
 
 
110
  )
111
 
112
  # Add a success message
113
+ st.success(f"All ZIP files have been successfully processed. JSON data has been flattened into a single object. You can now download the flattened JSON file or the Excel file.")