LLM generated output to be verified whether it is in json format. If not, LLM to be grounded and the prompt should rerun to regenerate the output in json format.
To upload files, please first save the app
import streamlit as st
import json
st.title("JSON Format Validator")
# Text area for input
user_input = st.text_area("Enter the text to validate as JSON:", height=300)
if st.button("Validate JSON"):
if user_input:
try:
# Try to parse the input as JSON
parsed_json = json.loads(user_input)
# If successful, display the parsed JSON in a nice format
st.success("✅ Valid JSON format!")
# Display the formatted JSON
st.subheader("Formatted JSON:")
st.json(parsed_json)
# Additional information about the JSON structure
st.subheader("JSON Structure Information:")
if isinstance(parsed_json, dict):
st.write(f"Type: Dictionary/Object with {len(parsed_json)} key-value pairs")
st.write("Top-level keys:", list(parsed_json.keys()))
elif isinstance(parsed_json, list):
st.write(f"Type: List/Array with {len(parsed_json)} elements")
else:
st.write(f"Type: {type(parsed_json).__name__}")
except json.JSONDecodeError as e:
# If JSON parsing fails, show the error
st.error(f"❌ Invalid JSON format!\nError: {str(e)}")
# Provide some common JSON formatting tips
st.warning("Common JSON formatting tips:")
st.markdown("""
- Strings must be enclosed in double quotes
- Use square brackets [] for arrays
- Use curly braces {} for objects
- Separate key-value pairs with commas
- Keys must be strings (in double quotes)
- No trailing commas allowed
""")
else:
st.warning("Please enter some text to validate.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?