Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
-
import jsonfix
|
| 4 |
import json5
|
| 5 |
|
| 6 |
-
# Function to attempt to fix JSON using
|
| 7 |
def fix_json(json_data):
|
| 8 |
try:
|
| 9 |
# First, try parsing with json5 which is more lenient
|
| 10 |
parsed_data = json5.loads(json_data)
|
| 11 |
-
|
| 12 |
# Try to pretty-print (indent) the parsed data
|
| 13 |
fixed_json = json.dumps(parsed_data, indent=4)
|
| 14 |
return fixed_json, "JSON fixed using json5."
|
| 15 |
-
|
| 16 |
except Exception as e:
|
| 17 |
-
|
| 18 |
-
try:
|
| 19 |
-
fixed_json = jsonfix.fix_data(json_data)
|
| 20 |
-
parsed_data = json.loads(fixed_json) # Validate it as valid JSON
|
| 21 |
-
fixed_json = json.dumps(parsed_data, indent=4)
|
| 22 |
-
return fixed_json, "JSON fixed using jsonfix."
|
| 23 |
-
|
| 24 |
-
except Exception as e:
|
| 25 |
-
return None, f"Failed to fix JSON: {str(e)}"
|
| 26 |
|
| 27 |
# Function to remove duplicates and fix JSON
|
| 28 |
def process_json(json_data):
|
|
@@ -38,17 +29,17 @@ def process_json(json_data):
|
|
| 38 |
try:
|
| 39 |
# Parse the fixed JSON string to a list of dictionaries
|
| 40 |
data = json.loads(fixed_json)
|
| 41 |
-
|
| 42 |
# Check if data is a list of dictionaries
|
| 43 |
if not isinstance(data, list):
|
| 44 |
return None, "Error: JSON is not a list of objects", None
|
| 45 |
|
| 46 |
# Remove duplicates by converting the list to a set of tuples, then back to a list of dicts
|
| 47 |
unique_data = [dict(t) for t in {tuple(d.items()) for d in data}]
|
| 48 |
-
|
| 49 |
# Convert the list back to JSON
|
| 50 |
cleaned_json = json.dumps(unique_data, indent=4)
|
| 51 |
-
|
| 52 |
return cleaned_json, f"JSON is valid and cleaned. {fix_message}", cleaned_json
|
| 53 |
|
| 54 |
except json.JSONDecodeError as e:
|
|
@@ -58,13 +49,13 @@ def process_json(json_data):
|
|
| 58 |
def process_file(uploaded_file):
|
| 59 |
# Read the content of the uploaded file directly
|
| 60 |
json_data = uploaded_file # This is already the content of the file as a string
|
| 61 |
-
|
| 62 |
# Process the JSON data
|
| 63 |
cleaned_json, message, download_json = process_json(json_data)
|
| 64 |
-
|
| 65 |
if message:
|
| 66 |
return cleaned_json, message, download_json
|
| 67 |
-
|
| 68 |
# Return the cleaned JSON for display and as a downloadable file
|
| 69 |
return cleaned_json, "JSON is valid and cleaned.", download_json
|
| 70 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
|
|
|
| 3 |
import json5
|
| 4 |
|
| 5 |
+
# Function to attempt to fix JSON using json5
|
| 6 |
def fix_json(json_data):
|
| 7 |
try:
|
| 8 |
# First, try parsing with json5 which is more lenient
|
| 9 |
parsed_data = json5.loads(json_data)
|
| 10 |
+
|
| 11 |
# Try to pretty-print (indent) the parsed data
|
| 12 |
fixed_json = json.dumps(parsed_data, indent=4)
|
| 13 |
return fixed_json, "JSON fixed using json5."
|
| 14 |
+
|
| 15 |
except Exception as e:
|
| 16 |
+
return None, f"Failed to fix JSON: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Function to remove duplicates and fix JSON
|
| 19 |
def process_json(json_data):
|
|
|
|
| 29 |
try:
|
| 30 |
# Parse the fixed JSON string to a list of dictionaries
|
| 31 |
data = json.loads(fixed_json)
|
| 32 |
+
|
| 33 |
# Check if data is a list of dictionaries
|
| 34 |
if not isinstance(data, list):
|
| 35 |
return None, "Error: JSON is not a list of objects", None
|
| 36 |
|
| 37 |
# Remove duplicates by converting the list to a set of tuples, then back to a list of dicts
|
| 38 |
unique_data = [dict(t) for t in {tuple(d.items()) for d in data}]
|
| 39 |
+
|
| 40 |
# Convert the list back to JSON
|
| 41 |
cleaned_json = json.dumps(unique_data, indent=4)
|
| 42 |
+
|
| 43 |
return cleaned_json, f"JSON is valid and cleaned. {fix_message}", cleaned_json
|
| 44 |
|
| 45 |
except json.JSONDecodeError as e:
|
|
|
|
| 49 |
def process_file(uploaded_file):
|
| 50 |
# Read the content of the uploaded file directly
|
| 51 |
json_data = uploaded_file # This is already the content of the file as a string
|
| 52 |
+
|
| 53 |
# Process the JSON data
|
| 54 |
cleaned_json, message, download_json = process_json(json_data)
|
| 55 |
+
|
| 56 |
if message:
|
| 57 |
return cleaned_json, message, download_json
|
| 58 |
+
|
| 59 |
# Return the cleaned JSON for display and as a downloadable file
|
| 60 |
return cleaned_json, "JSON is valid and cleaned.", download_json
|
| 61 |
|