File size: 3,313 Bytes
6fb15ba
41051ea
946e98d
6fb15ba
5ac6ff7
6fb15ba
2fddd9e
946e98d
2fddd9e
3db95b7
2adf8bd
6fb15ba
ed6902a
3db95b7
5ac6ff7
 
ed6902a
 
2fddd9e
3db95b7
2fddd9e
946e98d
 
 
26e452e
946e98d
26e452e
946e98d
 
 
 
 
 
2fddd9e
6fb15ba
2fddd9e
3db95b7
946e98d
26e452e
 
946e98d
26e452e
 
 
 
3db95b7
 
 
946e98d
26e452e
 
 
3db95b7
26e452e
6fb15ba
26e452e
 
 
 
 
ed6902a
3db95b7
 
57473f7
3db95b7
6fb15ba
2fddd9e
 
 
 
946e98d
 
 
26e452e
2fddd9e
26e452e
6fb15ba
26e452e
 
 
2fddd9e
 
6fb15ba
5ac6ff7
06ea8ea
5ac6ff7
06ea8ea
5ac6ff7
 
 
 
 
 
26e452e
5ac6ff7
946e98d
 
 
6fb15ba
2fddd9e
26e452e
 
 
 
2fddd9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import streamlit as st
import pandas as pd
import re
from io import BytesIO
import streamlit.components.v1 as components

# -------------------------------------------------
# Page Setup
# -------------------------------------------------
st.set_page_config(page_title="JSON Text to Excel", layout="wide")
st.title("πŸ“˜ JSON Text β†’ Excel Converter(Jaganath's Bot)")

st.markdown(
    "Paste JSON-like **text data** (not strict JSON). "
    "The app parses it line-by-line and generates Excel output. "
    "You can also copy the two columns to clipboard."
)

# -------------------------------------------------
# Text Input (HF Safe)
# -------------------------------------------------
if "json_text" not in st.session_state:
    st.session_state.json_text = ""

text_input = st.text_area(
    "JSON Data (Text Format)",
    height=380,
    key="json_text",
    placeholder='''"LABELS": {
  "ICN": {
    "ICN_SHIFT": "Shift Management"
  }
}'''
)

# -------------------------------------------------
# Line-by-line Parser (ROBUST)
# -------------------------------------------------
def parse_text_to_rows(text):
    rows = []

    for line in text.splitlines():
        line = line.strip()
        if not line:
            continue

        # Remove trailing commas
        line = line.rstrip(",")

        # Match "KEY": {
        obj_match = re.match(r'"([^"]+)"\s*:\s*\{', line)
        if obj_match:
            rows.append([obj_match.group(1), None])
            continue

        # Match "KEY": "VALUE"
        kv_match = re.match(r'"([^"]+)"\s*:\s*"([^"]*)"', line)
        if kv_match:
            rows.append([kv_match.group(1), kv_match.group(2)])
            continue

    return pd.DataFrame(
        rows,
        columns=["Object Name/Key Name", "Values"]
    )

# -------------------------------------------------
# Button Action
# -------------------------------------------------
if st.button("Generate Excel Data", type="primary"):
    raw_text = st.session_state.json_text

    if raw_text.strip() == "":
        st.warning("⚠️ Please paste JSON-like text data.")
    else:
        df = parse_text_to_rows(raw_text)

        if df.empty:
            st.error("❌ No valid key/value data found.")
        else:
            st.subheader("πŸ“Š Excel Preview")
            st.dataframe(df, use_container_width=True)

            # -------------------------
            # Copy to Clipboard Button (Only content)
            # -------------------------
            csv_text = df.to_csv(index=False, sep="\t", header=False)  # <-- header=False
            copy_button_html = f"""
            <button onclick="navigator.clipboard.writeText(`{csv_text}`)">πŸ“‹ Copy Two Columns</button>
            """
            components.html(copy_button_html, height=50)

            # -------------------------
            # Excel Export
            # -------------------------
            buffer = BytesIO()
            with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer:
                df.to_excel(writer, index=False, sheet_name="Labels")

            st.download_button(
                "πŸ“₯ Download Excel File",
                buffer.getvalue(),
                "JSON_Text_To_Excel.xlsx",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            )