diff --git a/README.md b/README.md index 4897c0f..f24ff6d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ A React-based web application for testing and validating JMESPath expressions ag - 🎯 **Real-time Evaluation**: JMESPath expressions are evaluated instantly as you type - 📝 **JSON Validation**: Built-in JSON syntax validation and error reporting -- 📁 **File Upload**: Load JSON data directly from local files +- 📁 **File Upload**: Load JSON data directly from local files (supports JSON Lines format for .log files) - 🎨 **Bootstrap UI**: Clean, responsive interface with Bootstrap styling - 🔄 **Sample Data**: Pre-loaded examples to get started quickly - 📱 **Responsive Design**: Works on desktop, tablet, and mobile devices @@ -75,7 +75,9 @@ npm run docker:run 1. **Enter a JMESPath expression** in the top input field (e.g., `people[*].name`) 2. **Add JSON data** using one of these methods: - - **Load from disk**: Click "📁 Load from Disk" to upload a JSON file + - **Load from disk**: Click "📁 Load from Disk" to upload files + - `.json` files: Standard JSON format + - `.log` files: JSON Lines format (each line is a JSON object, automatically converted to array) - **Paste or type**: Enter JSON data directly in the bottom-left textarea - **Load sample**: Use the "Load Sample" button for quick testing 3. **View the results** in the bottom-right output area diff --git a/src/App.js b/src/App.js index fd0ac41..4a93f6e 100644 --- a/src/App.js +++ b/src/App.js @@ -127,12 +127,35 @@ function App() { reader.onload = (e) => { try { const content = e.target.result; - // Try to parse as JSON to validate - JSON.parse(content); - setJsonData(content); + let jsonContent; + + // Handle .log files as sequences of JSON objects (JSON Lines format) + if (file.name.toLowerCase().endsWith('.log')) { + 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}`); + } + } + + jsonContent = JSON.stringify(jsonObjects, null, 2); + } else { + // Handle .json files as regular JSON + JSON.parse(content); // Validate JSON + jsonContent = content; + } + + setJsonData(jsonContent); setJsonError(''); } catch (err) { - setJsonError(`Invalid JSON file: ${err.message}`); + setJsonError(`Invalid file content: ${err.message}`); } }; reader.readAsText(file);