Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="JSON to Excel Converter", layout="wide")
|
| 8 |
+
|
| 9 |
+
st.title("JSON to Excel Converter")
|
| 10 |
+
|
| 11 |
+
# Text Area
|
| 12 |
+
json_text = st.text_area(
|
| 13 |
+
label="JSON Data",
|
| 14 |
+
height=300,
|
| 15 |
+
placeholder="Paste your JSON data here..."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def clean_json(text):
|
| 19 |
+
"""Remove trailing commas to avoid JSON errors"""
|
| 20 |
+
text = re.sub(r",\s*}", "}", text)
|
| 21 |
+
text = re.sub(r",\s*]", "]", text)
|
| 22 |
+
return text
|
| 23 |
+
|
| 24 |
+
def flatten_json(data):
|
| 25 |
+
rows = []
|
| 26 |
+
|
| 27 |
+
def recurse(obj):
|
| 28 |
+
if isinstance(obj, dict):
|
| 29 |
+
for k, v in obj.items():
|
| 30 |
+
rows.append({
|
| 31 |
+
"Object Name / Key Name": k,
|
| 32 |
+
"English_Values": None if isinstance(v, dict) else v
|
| 33 |
+
})
|
| 34 |
+
recurse(v)
|
| 35 |
+
|
| 36 |
+
recurse(data)
|
| 37 |
+
return rows
|
| 38 |
+
|
| 39 |
+
# Button
|
| 40 |
+
if st.button("Generate Excel Data"):
|
| 41 |
+
if not json_text.strip():
|
| 42 |
+
st.warning("Please paste JSON data first.")
|
| 43 |
+
else:
|
| 44 |
+
try:
|
| 45 |
+
cleaned_json = clean_json(json_text)
|
| 46 |
+
parsed_json = json.loads(cleaned_json)
|
| 47 |
+
|
| 48 |
+
flattened_rows = flatten_json(parsed_json)
|
| 49 |
+
df = pd.DataFrame(flattened_rows)
|
| 50 |
+
|
| 51 |
+
# Replace None with NULL
|
| 52 |
+
df["English_Values"] = df["English_Values"].fillna("NULL")
|
| 53 |
+
|
| 54 |
+
st.subheader("Excel Output (Copy & Paste Ready)")
|
| 55 |
+
st.dataframe(
|
| 56 |
+
df,
|
| 57 |
+
use_container_width=True,
|
| 58 |
+
hide_index=True
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# -------- Excel Export --------
|
| 62 |
+
output = BytesIO()
|
| 63 |
+
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
| 64 |
+
df.to_excel(writer, index=False, sheet_name="JSON_Data")
|
| 65 |
+
|
| 66 |
+
excel_data = output.getvalue()
|
| 67 |
+
|
| 68 |
+
st.download_button(
|
| 69 |
+
label="📥 Download Excel File",
|
| 70 |
+
data=excel_data,
|
| 71 |
+
file_name="JSON_To_Excel.xlsx",
|
| 72 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
st.error(f"Invalid JSON format ❌\n\n{e}")
|