JaganathC commited on
Commit
41051ea
·
verified ·
1 Parent(s): 6fb15ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -58
app.py CHANGED
@@ -1,76 +1,48 @@
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}")
 
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}")