Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,48 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
import json
|
| 4 |
-
import
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
-
st.set_page_config(
|
| 8 |
|
| 9 |
st.title("JSON to Excel Converter")
|
| 10 |
|
| 11 |
-
|
| 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
|
| 25 |
rows = []
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
rows.append(
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
recurse(v)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
return rows
|
| 38 |
|
| 39 |
-
# Button
|
| 40 |
if st.button("Generate Excel Data"):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
| 55 |
-
st.dataframe(
|
| 56 |
-
df,
|
| 57 |
-
use_container_width=True,
|
| 58 |
-
hide_index=True
|
| 59 |
-
)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
| 64 |
-
df.to_excel(writer, index=False, sheet_name="JSON_Data")
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import json
|
| 3 |
+
import pandas as pd
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
st.set_page_config(layout="wide")
|
| 7 |
|
| 8 |
st.title("JSON to Excel Converter")
|
| 9 |
|
| 10 |
+
json_text = st.text_area("JSON Data", height=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def process_json(data):
|
| 13 |
rows = []
|
| 14 |
|
| 15 |
+
def traverse(obj):
|
| 16 |
+
for k, v in obj.items():
|
| 17 |
+
if isinstance(v, dict):
|
| 18 |
+
rows.append([k, None])
|
| 19 |
+
traverse(v)
|
| 20 |
+
else:
|
| 21 |
+
rows.append([k, v])
|
|
|
|
| 22 |
|
| 23 |
+
traverse(data)
|
| 24 |
+
return pd.DataFrame(rows, columns=["Object Name/Key Name", "English_Values"])
|
| 25 |
|
|
|
|
| 26 |
if st.button("Generate Excel Data"):
|
| 27 |
+
try:
|
| 28 |
+
cleaned = json_text.replace(",}", "}").replace(",]", "]")
|
| 29 |
+
parsed = json.loads(cleaned)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
df = process_json(parsed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
st.subheader("Excel Preview")
|
| 34 |
+
st.dataframe(df, use_container_width=True)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
output = BytesIO()
|
| 37 |
+
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
| 38 |
+
df.to_excel(writer, index=False, sheet_name="Labels")
|
| 39 |
|
| 40 |
+
st.download_button(
|
| 41 |
+
"Download Excel File",
|
| 42 |
+
data=output.getvalue(),
|
| 43 |
+
file_name="JSON_To_Excel.xlsx",
|
| 44 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error(f"Invalid JSON: {e}")
|