Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import pandas as pd
|
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
# -------------------------------------------------
|
| 7 |
-
# Page
|
| 8 |
# -------------------------------------------------
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="JSON to Excel Converter",
|
|
@@ -13,17 +13,22 @@ st.set_page_config(
|
|
| 13 |
|
| 14 |
st.title("📘 JSON to Excel Converter")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# -------------------------------------------------
|
| 17 |
-
#
|
| 18 |
# -------------------------------------------------
|
| 19 |
json_input = st.text_area(
|
| 20 |
"JSON Data",
|
| 21 |
height=350,
|
| 22 |
-
placeholder="
|
| 23 |
)
|
| 24 |
|
| 25 |
# -------------------------------------------------
|
| 26 |
-
# JSON
|
| 27 |
# -------------------------------------------------
|
| 28 |
def flatten_json(data):
|
| 29 |
rows = []
|
|
@@ -37,6 +42,7 @@ def flatten_json(data):
|
|
| 37 |
rows.append([key, value])
|
| 38 |
|
| 39 |
walk(data)
|
|
|
|
| 40 |
return pd.DataFrame(
|
| 41 |
rows,
|
| 42 |
columns=["Object Name/Key Name", "English_Values"]
|
|
@@ -47,23 +53,26 @@ def flatten_json(data):
|
|
| 47 |
# -------------------------------------------------
|
| 48 |
if st.button("Generate Excel Data", type="primary"):
|
| 49 |
if not json_input.strip():
|
| 50 |
-
st.warning("Please paste JSON data.")
|
| 51 |
else:
|
| 52 |
try:
|
|
|
|
|
|
|
| 53 |
# Fix trailing commas
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
parsed_json = json.loads(cleaned_json)
|
| 61 |
df = flatten_json(parsed_json)
|
| 62 |
|
| 63 |
st.subheader("📊 Excel Preview")
|
| 64 |
st.dataframe(df, use_container_width=True)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
excel_buffer = BytesIO()
|
| 68 |
with pd.ExcelWriter(excel_buffer, engine="xlsxwriter") as writer:
|
| 69 |
df.to_excel(
|
|
@@ -80,4 +89,4 @@ if st.button("Generate Excel Data", type="primary"):
|
|
| 80 |
)
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
st.error(f"Invalid JSON
|
|
|
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
# -------------------------------------------------
|
| 7 |
+
# Page Configuration
|
| 8 |
# -------------------------------------------------
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="JSON to Excel Converter",
|
|
|
|
| 13 |
|
| 14 |
st.title("📘 JSON to Excel Converter")
|
| 15 |
|
| 16 |
+
st.markdown(
|
| 17 |
+
"Paste your JSON label data below. "
|
| 18 |
+
"The app will flatten keys and export them as an Excel file."
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
# -------------------------------------------------
|
| 22 |
+
# JSON Input
|
| 23 |
# -------------------------------------------------
|
| 24 |
json_input = st.text_area(
|
| 25 |
"JSON Data",
|
| 26 |
height=350,
|
| 27 |
+
placeholder='Example:\n"LABELS": { "ICN": { "ICN_SHIFT": "Shift Management" } }'
|
| 28 |
)
|
| 29 |
|
| 30 |
# -------------------------------------------------
|
| 31 |
+
# JSON Flattening Logic
|
| 32 |
# -------------------------------------------------
|
| 33 |
def flatten_json(data):
|
| 34 |
rows = []
|
|
|
|
| 42 |
rows.append([key, value])
|
| 43 |
|
| 44 |
walk(data)
|
| 45 |
+
|
| 46 |
return pd.DataFrame(
|
| 47 |
rows,
|
| 48 |
columns=["Object Name/Key Name", "English_Values"]
|
|
|
|
| 53 |
# -------------------------------------------------
|
| 54 |
if st.button("Generate Excel Data", type="primary"):
|
| 55 |
if not json_input.strip():
|
| 56 |
+
st.warning("⚠️ Please paste JSON data.")
|
| 57 |
else:
|
| 58 |
try:
|
| 59 |
+
raw = json_input.strip()
|
| 60 |
+
|
| 61 |
# Fix trailing commas
|
| 62 |
+
raw = raw.replace(",}", "}").replace(",]", "]")
|
| 63 |
+
|
| 64 |
+
# Auto-wrap if root object missing
|
| 65 |
+
if not raw.startswith("{"):
|
| 66 |
+
raw = "{" + raw + "}"
|
| 67 |
+
|
| 68 |
+
parsed_json = json.loads(raw)
|
| 69 |
|
|
|
|
| 70 |
df = flatten_json(parsed_json)
|
| 71 |
|
| 72 |
st.subheader("📊 Excel Preview")
|
| 73 |
st.dataframe(df, use_container_width=True)
|
| 74 |
|
| 75 |
+
# Generate Excel file
|
| 76 |
excel_buffer = BytesIO()
|
| 77 |
with pd.ExcelWriter(excel_buffer, engine="xlsxwriter") as writer:
|
| 78 |
df.to_excel(
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
except Exception as e:
|
| 92 |
+
st.error(f"❌ Invalid JSON: {e}")
|