import React, { useState } from 'react'; import { Code, AlertCircle, CheckCircle, Edit3 } from 'lucide-react'; import ToolLayout from '../components/ToolLayout'; import CopyButton from '../components/CopyButton'; import StructuredEditor from '../components/StructuredEditor'; const JsonTool = () => { const [input, setInput] = useState(''); const [output, setOutput] = useState(''); const [error, setError] = useState(''); const [isValid, setIsValid] = useState(null); const [editorMode, setEditorMode] = useState('text'); // 'text' or 'visual' const [structuredData, setStructuredData] = useState({}); const formatJson = () => { try { const parsed = JSON.parse(input); const formatted = JSON.stringify(parsed, null, 2); setOutput(formatted); setError(''); setIsValid(true); } catch (err) { setError(`Invalid JSON: ${err.message}`); setOutput(''); setIsValid(false); } }; const minifyJson = () => { try { const parsed = JSON.parse(input); const minified = JSON.stringify(parsed); setOutput(minified); setError(''); setIsValid(true); } catch (err) { setError(`Invalid JSON: ${err.message}`); setOutput(''); setIsValid(false); } }; const validateJson = () => { try { JSON.parse(input); setError(''); setIsValid(true); setOutput('✅ Valid JSON'); } catch (err) { setError(`Invalid JSON: ${err.message}`); setIsValid(false); setOutput(''); } }; const clearAll = () => { setInput(''); setOutput(''); setError(''); setIsValid(null); }; const handleStructuredDataChange = (newData) => { setStructuredData(newData); setInput(JSON.stringify(newData, null, 2)); setError(''); setIsValid(true); }; const switchToVisualEditor = () => { try { const parsed = input ? JSON.parse(input) : {}; setStructuredData(parsed); setEditorMode('visual'); setError(''); setIsValid(true); } catch (err) { setError(`Cannot switch to visual editor: ${err.message}`); setIsValid(false); } }; const switchToTextEditor = () => { setEditorMode('text'); }; const loadSample = () => { const sample = { "name": "John Doe", "age": 30, "email": "john@example.com", "address": { "street": "123 Main St", "city": "New York", "zipCode": "10001" }, "hobbies": ["reading", "coding", "traveling"], "isActive": true }; setInput(JSON.stringify(sample, null, 2)); setStructuredData(sample); }; return ( {/* Editor Mode Toggle */}
{/* Controls */}
{/* Status Indicator */} {isValid !== null && (
{isValid ? ( ) : ( )} {isValid ? 'Valid JSON' : 'Invalid JSON'}
)} {/* Error Display */} {error && (

Error

{error}

)} {/* Input/Output Grid */}
{/* Input */}
{editorMode === 'text' ? (