Add JSON Lines support for .log files

- Handle .log files as sequences of JSON objects (JSON Lines/NDJSON format)
- Convert each line to JSON object and combine into array (like jq -s)
- Maintain existing .json file handling for standard JSON format
- Add detailed error messages for invalid JSON lines
- Update documentation to explain both file format supports
This commit is contained in:
2026-01-18 13:56:02 +01:00
parent 394b7e279c
commit d9114bf48e
2 changed files with 31 additions and 6 deletions

View File

@@ -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 - 🎯 **Real-time Evaluation**: JMESPath expressions are evaluated instantly as you type
- 📝 **JSON Validation**: Built-in JSON syntax validation and error reporting - 📝 **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 - 🎨 **Bootstrap UI**: Clean, responsive interface with Bootstrap styling
- 🔄 **Sample Data**: Pre-loaded examples to get started quickly - 🔄 **Sample Data**: Pre-loaded examples to get started quickly
- 📱 **Responsive Design**: Works on desktop, tablet, and mobile devices - 📱 **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`) 1. **Enter a JMESPath expression** in the top input field (e.g., `people[*].name`)
2. **Add JSON data** using one of these methods: 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 - **Paste or type**: Enter JSON data directly in the bottom-left textarea
- **Load sample**: Use the "Load Sample" button for quick testing - **Load sample**: Use the "Load Sample" button for quick testing
3. **View the results** in the bottom-right output area 3. **View the results** in the bottom-right output area

View File

@@ -127,12 +127,35 @@ function App() {
reader.onload = (e) => { reader.onload = (e) => {
try { try {
const content = e.target.result; const content = e.target.result;
// Try to parse as JSON to validate let jsonContent;
JSON.parse(content);
setJsonData(content); // 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(''); setJsonError('');
} catch (err) { } catch (err) {
setJsonError(`Invalid JSON file: ${err.message}`); setJsonError(`Invalid file content: ${err.message}`);
} }
}; };
reader.readAsText(file); reader.readAsText(file);