import React, { useState, useEffect } from 'react'; import jmespath from 'jmespath'; import './App.css'; // JMESPath Testing Tool - Main Application Component function App() { const [jmespathExpression, setJmespathExpression] = useState('people[0].name'); const [theme, setTheme] = useState(() => { // Load theme from localStorage or default to 'auto' return localStorage.getItem('theme') || 'auto'; }); const [jsonData, setJsonData] = useState(`{ "people": [ { "name": "John Doe", "age": 30, "city": "New York" }, { "name": "Jane Smith", "age": 25, "city": "Los Angeles" } ], "total": 2 }`); const [result, setResult] = useState(''); const [error, setError] = useState(''); const [jsonError, setJsonError] = useState(''); // Theme management useEffect(() => { // Apply theme to document const applyTheme = (selectedTheme) => { const root = document.documentElement; const body = document.body; // Clear existing theme classes from both html and body root.className = ''; body.classList.remove('theme-light', 'theme-dark'); if (selectedTheme === 'light') { body.classList.add('theme-light'); } else if (selectedTheme === 'dark') { body.classList.add('theme-dark'); } // 'auto' uses CSS media queries (no class needed) }; applyTheme(theme); localStorage.setItem('theme', theme); }, [theme]); const handleThemeChange = (newTheme) => { setTheme(newTheme); }; const evaluateExpression = () => { try { // Clear previous errors setError(''); setJsonError(''); // Validate and parse JSON let parsedData; try { parsedData = JSON.parse(jsonData); } catch (jsonErr) { setJsonError(`Invalid JSON: ${jsonErr.message}`); setResult(''); return; } // Evaluate JMESPath expression const queryResult = jmespath.search(parsedData, jmespathExpression); // Format the result if (queryResult === null || queryResult === undefined) { setResult('null'); } else { setResult(JSON.stringify(queryResult, null, 2)); } } catch (jmesErr) { setError(`JMESPath Error: ${jmesErr.message}`); setResult(''); } }; // Auto-evaluate when inputs change useEffect(() => { if (jmespathExpression && jsonData) { evaluateExpression(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [jmespathExpression, jsonData]); const handleJmespathChange = (e) => { setJmespathExpression(e.target.value); }; const handleJsonChange = (e) => { setJsonData(e.target.value); }; const formatJson = () => { try { const parsed = JSON.parse(jsonData); setJsonData(JSON.stringify(parsed, null, 2)); } catch (err) { // If JSON is invalid, don't format } }; const clearAll = () => { setJmespathExpression(''); setJsonData(''); setResult(''); setError(''); setJsonError(''); }; const loadSample = () => { setJmespathExpression('people[*].name'); setJsonData(`{ "people": [ { "name": "Alice Johnson", "age": 28, "city": "Chicago", "skills": ["JavaScript", "React", "Node.js"] }, { "name": "Bob Wilson", "age": 35, "city": "Seattle", "skills": ["Python", "Django", "PostgreSQL"] }, { "name": "Carol Davis", "age": 32, "city": "Austin", "skills": ["Java", "Spring", "MySQL"] } ], "total": 3, "department": "Engineering" }`); }; const loadFromDisk = () => { const fileInput = document.createElement('input'); fileInput.type = 'file'; fileInput.accept = '.json'; fileInput.onchange = (event) => { const file = event.target.files[0]; if (file) { const reader = new FileReader(); reader.onload = (e) => { try { const content = e.target.result; // Handle .json files as regular JSON JSON.parse(content); // Validate JSON setJsonData(content); setJsonError(''); } catch (err) { setJsonError(`Invalid JSON file: ${err.message}`); } }; reader.readAsText(file); } }; fileInput.click(); }; const loadLogFile = () => { const fileInput = document.createElement('input'); fileInput.type = 'file'; fileInput.accept = '.log'; fileInput.onchange = (event) => { const file = event.target.files[0]; if (file) { const reader = new FileReader(); reader.onload = (e) => { try { const content = e.target.result; const lines = content.split('\n') .map(line => line.trim()) .filter(line => line.length > 0); const jsonObjects = []; for (const line of lines) { try { const obj = JSON.parse(line); jsonObjects.push(obj); } catch (lineError) { throw new Error(`Invalid JSON on line: "${line.substring(0, 50)}..." - ${lineError.message}`); } } const jsonContent = JSON.stringify(jsonObjects, null, 2); setJsonData(jsonContent); setJsonError(''); } catch (err) { setJsonError(`Invalid log file: ${err.message}`); } }; reader.readAsText(file); } }; fileInput.click(); }; return (
{/* Top Section: Title only */}

JMESPath Testing Tool

{/* Theme switcher */}
{/* Main Content Section - flex-grow to fill space */}
{/* Description paragraph */}

Validate and test JMESPath expressions against JSON data in real-time. Enter your JMESPath query and JSON data below to see the results instantly.

{/* Middle Section: JMESPath Expression Input */}
JMESPath Expression
{error || 'Expression is correct'}
{/* Lower Middle Sections: JSON Data (left) and Query Result (right) */}
{/* Lower Middle Left Section: JSON Data Input */}
JSON Data