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 [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(''); 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,.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; // Try to parse as JSON to validate JSON.parse(content); setJsonData(content); setJsonError(''); } catch (err) { setJsonError(`Invalid JSON file: ${err.message}`); } }; reader.readAsText(file); } }; fileInput.click(); }; return (
{/* Header Section */}

JMESPath Testing Tool

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

{/* JMESPath Expression Input - Middle Section */}
JMESPath Expression
{error && (
{error}
)}
{/* Bottom Row - JSON Input and Results */}
JSON Data