Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 64 |
|
| 65 |
-
if
|
| 66 |
-
#
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Create a DataFrame from the flattened JSON data
|
| 70 |
-
df = pd.DataFrame([
|
| 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(
|
| 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("
|
|
|
|
| 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.")
|