Creating a JSON Viewer Pro with multiple visualization options involves designing interactive interfaces to display JSON data in different ways. Below is a simple example of such a viewer using HTML, CSS, and JavaScript. This includes a basic tree view and a simple chart example. For a full-featured application, you might want to integrate libraries for more advanced visualizations like D3.js or Chart.js.
Here's a simple example using HTML, CSS, and JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Viewer Pro</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#jsonInput {
width: 100%;
height: 100px;
}
#output {
margin-top: 20px;
}
.tree-view {
white-space: pre-wrap;
font-family: monospace;
}
.chart {
width: 100%;
height: 300px;
}
</style>
</head>
<body>
<h1>JSON Viewer Pro</h1>
<textarea id="jsonInput" placeholder="Paste your JSON here"></textarea>
<button onclick="viewJSON()">View JSON</button>
<div id="output">
<!-- Views will be dynamically inserted here -->
</div>
<script>
function viewJSON() {
const jsonInput = document.getElementById('jsonInput').value;
const output = document.getElementById('output');
try {
const jsonData = JSON.parse(jsonInput);
output.innerHTML = '';
// Tree View
const treeView = document.createElement('div');
treeView.className = 'tree-view';
treeView.textContent = JSON.stringify(jsonData, null, 2);
output.appendChild(treeView);
// Simple Bar Chart View (using inline SVG)
const chartData = Array.isArray(jsonData) ? jsonData : Object.values(jsonData);
const maxChartValue = Math.max(...chartData.filter(val => typeof val === 'number'));
const chart = document.createElement('div');
chart.className = 'chart';
chart.innerHTML = chartData.filter(val => typeof val === 'number').map(val => {
const height = (val / maxChartValue) * 100;
return `<div style="display: inline-block; width: 20px; height: 100%; margin-right: 5px; background-color: blue;">
<div style="width: 100%; height: ${height}%; background-color: lightblue;"></div>
</div>`;
}).join('');
output.appendChild(chart);
// Here you would add additional visualizations using libraries or custom code
} catch (e) {
output.innerHTML = '<p style="color: red;">Invalid JSON: ' + e.message + '</p>';
}
}
</script>
</body>
</html>
Explanation:
-
Input Area: A simple <textarea> to input JSON data.
-
Tree View: Displays formatted JSON text using JSON.stringify() with indentation, mimicking a simple tree structure.
-
Basic Chart: A simple inline bar chart example using <div> elements, assuming JSON values are numbers. For a more sophisticated chart, consider using libraries like Chart.js or D3.js.
For a more comprehensive application, consider:
- Collapsible Tree: Using a more advanced library or creating collapsible sections for nested objects/arrays.
- Editable JSON: Allowing users to edit JSON data through a visual editor.
- Tabular View: Displaying JSON arrays in a tabular format.
- Validation: Adding features to validate JSON format dynamically.
This example sets up a basic structure you can expand upon with more complex features and design elements.