gemini-2.5-pro· Google LMArena Human Preference 140K
how do I print the json output in more friendly tabular format ? consider its a desktop…
- User
how do I print the json output in more friendly tabular format ? consider its a desktop ui with full web page accessible for table once API request has been made ----
<main class="app-main"> <div class="request-form"> <div class="form-group"> <label for="apiUrl">API Endpoint</label> <input type="text" id="apiUrl" v-model="apiUrl" placeholder="http://localhost:8080/descriptive" class="form-input" /> </div> <div class="form-group"> <label for="datasetPath">Dataset Path</label> <input type="text" id="datasetPath" v-model="datasetPath" placeholder="G:\projects\uv_rup99\handyscripts\products-100.csv" class="form-input" /> </div> <div class="form-group"> <label for="column">Column</label> <input type="text" id="column" v-model="column" placeholder="Price" class="form-input" /> </div> <button @click="fetchData" :disabled="isLoading" class="submit-button" > <span v-if="!isLoading">Send Request</span> <span v-else class="loading-indicator"> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> </span> </button> </div> <div class="results-container"> <div v-if="statusMessage" class="status-message" :class="statusType"> <div class="status-icon"> <svg v-if="statusType === 'success'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> <path fill="currentColor" d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4M11,16.5L6.5,12L7.91,10.59L11,13.67L16.59,8.09L18,9.5L11,16.5Z" /> </svg> <svg v-else-if="statusType === 'error'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> <path fill="currentColor" d="M12,2C6.47,2 2,6.47 2,12C2,17.53 6.47,22 12,22C17.53,22 22,17.53 22,12C22,6.47 17.53,2 12,2M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 17.59,20 12,20M12,6C10.9,6 10,6.9 10,8C10,9.1 10.9,10 12,10C13.1,10 14,9.1 14,8C14,6.9 13.1,6 12,6M12,16C10.9,16 10,15.1 10,14C10,12.9 10.9,12 12,12C13.1,12 14,12.9 14,14C14,15.1 13.1,16 12,16Z" /> </svg> <svg v-else xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> <path fill="currentColor" d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4M12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9Z" /> </svg> </div> <div class="status-content"> <p>{{ statusMessage }}</p> <div v-if="fullUrl" class="url-display"> <span class="url-label">Request URL:</span> <code class="url-text">{{ fullUrl }}</code> </div> </div> </div> <div v-if="results" class="result-section"> <h3>Response Preview</h3> <div class="json-container"> <highlightjs language="json" :code="formattedResults" class="json-highlight" /> </div> <button @click="copyToClipboard" class="copy-button"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16"> <path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H10A2,2 0 0,0 8,3V5H6A2,2 0 0,0 4,7V19A2,2 0 0,0 6,21H18A2,2 0 0,0 20,19V10H18V8A2,2 0 0,0 16,6V3A2,2 0 0,0 16,1Z" /> </svg> Copy to Clipboard </button> </div> <div v-if="errorDetails" class="error-section"> <h3>Error Details</h3> <div class="error-content"> <highlightjs language="json" :code="formattedError" class="json-highlight" /> </div> </div> </div> </main> <footer class="app-footer"> <p>API Client © {{ new Date().getFullYear() }}</p> </footer> - Assistant
Excellent idea. Displaying the key-value data from your descriptive analytics API in a table is a much more user-friendly approach.
Here is how you can update your
App.vuecomponent to include a clean, readable table view for the results, while still keeping the raw JSON view available.Summary of Changes:
- View Toggling: We'll add buttons to switch between a "Table View" and a "JSON View".
- Table Component: A new, beautifully styled HTML
<table>will be added to display the results. - Computed Property for Table Data: We'll create a computed property to format the JSON data into a simple array of
{ key, value }objects, which is easier to loop over. - New CSS: We'll add styles to make the table and the view-toggle buttons look professional and integrated with your existing design.
Updated
App.vueFileReplace the entire content of your
App.vuefile with this new version. I've added comments prefixed with<!-- NEW -->or// NEWto highlight the changes.<template> <div class="app-container"> <header class="app-header"> <h1>API Request Client</h1> <p class="subtitle">Query descriptive analytics from your API endpoint</p> </header> <main class="app-main"> <!-- Request Form (Unchanged) --> <div class="request-form"> <!-- ... form inputs and button ... --> <div class="form-group"> <label for="apiUrl">API Endpoint</label> <input type="text" id="apiUrl" v-model="apiUrl" placeholder="http://localhost:8080/descriptive" class="form-input" /> </div> <div class="form-group"> <label for="datasetPath">Dataset Path</label> <input type="text" id="datasetPath" v-model="datasetPath" placeholder="G:\projects\uv_rup99\handyscripts\products-100.csv" class="form-input" /> </div> <div class="form-group"> <label for="column">Column</label> <input type="text" id="column" v-model="column" placeholder="Price" class="form-input" /> </div> <button @click="fetchData" :disabled="isLoading" class="submit-button" > <span v-if="!isLoading">Send Request</span> <span v-else class="loading-indicator"> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> </span> </button> </div> <div class="results-container"> <!-- Status Message (Unchanged) --> <div v-if="statusMessage" class="status-message" :class="statusType"> <!-- ... status icons and content ... --> <div class="status-icon"> <svg v-if="statusType === 'success'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> <path fill="currentColor" d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4M11,16.5L6.5,12L7.91,10.59L11,13.67L16.59,8.09L18,9.5L11,16.5Z" /> </svg> <svg v-else-if="statusType === 'error'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> <path fill="currentColor" d="M12,2C6.47,2 2,6.47 2,12C2,17.53 6.47,22 12,22C17.53,22 22,17.53 22,12C22,6.47 17.53,2 12,2M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 17.59,20 12,20M12,6C10.9,6 10,6.9 10,8C10,9.1 10.9,10 12,10C13.1,10 14,9.1 14,8C14,6.9 13.1,6 12,6M12,16C10.9,16 10,15.1 10,14C10,12.9 10.9,12 12,12C13.1,12 14,12.9 14,14C14,15.1 13.1,16 12,16Z" /> </svg> <svg v-else xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> <path fill="currentColor" d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4M12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9Z" /> </svg> </div> <div class="status-content"> <p>{{ statusMessage }}</p> <div v-if="fullUrl" class="url-display"> <span class="url-label">Request URL:</span> <code class="url-text">{{ fullUrl }}</code> </div> </div> </div> <!-- NEW: Updated Result Section with View Toggler --> <div v-if="results" class="result-section"> <div class="result-header"> <h3>Response Preview</h3> <!-- NEW: View mode toggler --> <div class="view-toggle"> <button @click="viewMode = 'table'" :class="{ active: viewMode === 'table' }">Table</button> <button @click="viewMode = 'json'" :class="{ active: viewMode === 'json' }">JSON</button> </div> </div> <!-- NEW: Table View --> <div v-if="viewMode === 'table'" class="table-container"> <table class="results-table"> <thead> <tr> <th>Statistic</th> <th>Value</th> </tr> </thead> <tbody> <tr v-for="item in tableData" :key="item.key"> <td>{{ item.key }}</td> <td>{{ formatValue(item.value) }}</td> </tr> </tbody> </table> </div> <!-- Existing JSON View --> <div v-if="viewMode === 'json'"> <div class="json-container"> <highlightjs language="json" :code="formattedResults" class="json-highlight" /> </div> <button @click="copyToClipboard" class="copy-button"> <!-- ... copy icon ... --> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16"> <path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H10A2,2 0 0,0 8,3V5H6A2,2 0 0,0 4,7V19A2,2 0 0,0 6,21H18A2,2 0 0,0 20,19V10H18V8A2,2 0 0,0 16,6V3A2,2 0 0,0 16,1Z" /> </svg> Copy JSON to Clipboard </button> </div> </div> <!-- Error Details (Unchanged) --> <div v-if="errorDetails" class="error-section"> <h3>Error Details</h3> <div class="error-content"> <highlightjs language="json" :code="formattedError" class="json-highlight" /> </div> </div> </div> </main> <footer class="app-footer"> <p>API Client © {{ new Date().getFullYear() }}</p> </footer> </div> </template> <script setup> import { ref, computed } from 'vue'; import { Highlightjs } from 'vue-highlightjs'; import 'highlight.js/styles/atom-one-dark.css'; const apiUrl = ref('http://localhost:8080/descriptive'); const datasetPath = ref('G:\\projects\\uv_rup99\\handyscripts\\products-100.csv'); const column = ref('Price'); const fullUrl = ref(''); const statusMessage = ref(''); const statusType = ref('info'); const results = ref(null); const errorDetails = ref(null); const isLoading = ref(false); // NEW: State to manage the view mode (table or JSON) const viewMode = ref('table'); // --- Computed Properties --- const formattedResults = computed(() => { return results.value ? JSON.stringify(results.value, null, 2) : ''; }); // NEW: Computed property to transform the results object into an array for the table const tableData = computed(() => { if (!results.value || typeof results.value !== 'object') return []; return Object.entries(results.value).map(([key, value]) => ({ key, value })); }); const formattedError = computed(() => { if (!errorDetails.value) return ''; if (typeof errorDetails.value === 'object') { return JSON.stringify(errorDetails.value, null, 2); } return errorDetails.value; }); // NEW: Helper function to format values for display in the table const formatValue = (value) => { if (typeof value === 'number') { // Format numbers to have commas and up to 4 decimal places for precision return value.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 4, }); } if (value === null) return 'null'; if (value === undefined) return 'undefined'; return value.toString(); }; // --- Methods --- const fetchData = async () => { isLoading.value = true; statusMessage.value = ''; results.value = null; errorDetails.value = null; fullUrl.value = ''; viewMode.value = 'table'; // NEW: Default to table view on new request try { const encodedDatasetPath = encodeURIComponent(datasetPath.value); const encodedColumn = encodeURIComponent(column.value); const constructedUrl = `${apiUrl.value}?datasetPath=${encodedDatasetPath}&column=${encodedColumn}`; fullUrl.value = constructedUrl; statusMessage.value = `Querying URL...`; statusType.value = 'info'; const response = await fetch(constructedUrl, { method: 'GET', headers: { 'Accept': 'application/json' }, }); if (!response.ok) { const error = new Error('HTTP error'); error.response = response; throw error; } const data = await response.json(); if (data && Object.keys(data).length > 0) { results.value = data; statusMessage.value = 'Request successful!'; statusType.value = 'success'; } else { statusMessage.value = 'Request successful, but the API returned an empty response.'; statusType.value = 'warning'; results.value = data; } } catch (error) { await handleApiError(error); } finally { isLoading.value = false; } }; const handleApiError = async (error) => { statusType.value = 'error'; console.error("API Request Failed:", error); if (error.response) { const response = error.response; statusMessage.value = `API request failed with status: ${response.status} ${response.statusText}`; let errorBody = 'Could not read error body.'; try { const text = await response.text(); try { errorBody = JSON.parse(text); } catch (e) { errorBody = text; } } catch (readError) { console.error("Could not read the error response body:", readError); } errorDetails.value = { statusCode: response.status, statusText: response.statusText, body: errorBody, }; } else { statusMessage.value = 'A client-side error occurred.'; if (error.message.includes('Failed to fetch')) { statusMessage.value += ' (Check network connection or server\'s CORS policy)'; } else if (error instanceof SyntaxError) { statusMessage.value = 'Failed to parse a successful server response as JSON.'; } errorDetails.value = { type: error.name, message: error.message, stack: error.stack, }; } }; const copyToClipboard = () => { if (!results.value) return; navigator.clipboard.writeText(formattedResults.value) .then(() => { statusMessage.value = 'Copied to clipboard!'; statusType.value = 'success'; setTimeout(() => { if (results.value) { statusMessage.value = 'Request successful!'; } }, 2000); }) .catch(err => { console.error('Failed to copy: ', err); statusMessage.value = 'Failed to copy to clipboard'; statusType.value = 'error'; }); }; </script> <style> /* ... existing styles ... */ :root { --primary-color: #4361ee; --primary-dark: #3a56d4; --success-color: #3bb273; --error-color: #f5365c; --warning-color: #ffd166; --info-color: #11cdef; --text-color: #333; --light-text: #777; --background: #f8f9fa; --card-bg: #ffffff; --border-color: #e9ecef; --shadow: 0 4px 6px rgba(0, 0, 0, 0.1); --radius: 8px; } * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; line-height: 1.6; color: var(--text-color); background-color: var(--background); } .app-container { display: flex; flex-direction: column; min-height: 100vh; max-width: 1200px; margin: 0 auto; padding: 20px; } .app-header { text-align: center; margin-bottom: 30px; padding: 20px 0; } .app-header h1 { color: var(--primary-color); font-size: 2.2rem; margin-bottom: 10px; } .subtitle { color: var(--light-text); font-size: 1.1rem; } .app-main { flex: 1; display: flex; flex-direction: column; gap: 30px; } .request-form { background: var(--card-bg); border-radius: var(--radius); padding: 30px; box-shadow: var(--shadow); } .form-group { margin-bottom: 20px; } .form-group label { display: block; margin-bottom: 8px; font-weight: 600; color: var(--text-color); } .form-input { width: 100%; padding: 12px 15px; border: 1px solid var(--border-color); border-radius: var(--radius); font-size: 1rem; transition: border-color 0.3s ease; } .form-input:focus { outline: none; border-color: var(--primary-color); box-shadow: 0 0 0 2px rgba(67, 97, 238, 0.2); } .submit-button { width: 100%; padding: 14px; background-color: var(--primary-color); color: white; border: none; border-radius: var(--radius); font-size: 1rem; font-weight: 600; cursor: pointer; transition: all 0.3s ease; display: flex; justify-content: center; align-items: center; } .submit-button:hover:not(:disabled) { background-color: var(--primary-dark); transform: translateY(-2px); } .submit-button:disabled { background-color: #cccccc; cursor: not-allowed; transform: none; } .loading-indicator { display: flex; gap: 5px; } .dot { width: 8px; height: 8px; background-color: white; border-radius: 50%; animation: bounce 1.4s infinite ease-in-out; } .dot:nth-child(1) { animation-delay: -0.32s; } .dot:nth-child(2) { animation-delay: -0.16s; } @keyframes bounce { 0%, 80%, 100% { transform: scale(0); } 40% { transform: scale(1); } } .results-container { flex: 1; } .status-message { display: flex; align-items: flex-start; gap: 15px; padding: 15px 20px; border-radius: var(--radius); margin-bottom: 20px; } .status-message.info { background-color: rgba(17, 205, 239, 0.1); border-left: 4px solid var(--info-color); } .status-message.success { background-color: rgba(59, 178, 115, 0.1); border-left: 4px solid var(--success-color); } .status-message.warning { background-color: rgba(255, 209, 102, 0.1); border-left: 4px solid var(--warning-color); } .status-message.error { background-color: rgba(245, 54, 92, 0.1); border-left: 4px solid var(--error-color); } .status-icon { display: flex; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: 50%; } .status-message.info .status-icon { color: var(--info-color); } .status-message.success .status-icon { color: var(--success-color); } .status-message.warning .status-icon { color: var(--warning-color); } .status-message.error .status-icon { color: var(--error-color); } .status-content { flex: 1; } .url-display { margin-top: 10px; padding: 10px; background-color: rgba(0, 0, 0, 0.05); border-radius: var(--radius); font-family: monospace; word-break: break-all; } .url-label { display: block; font-size: 0.9rem; color: var(--light-text); margin-bottom: 5px; } .url-text { color: var(--primary-color); font-size: 0.9rem; } .result-section, .error-section { background: var(--card-bg); border-radius: var(--radius); padding: 25px; box-shadow: var(--shadow); margin-bottom: 20px; } /* NEW: Styles for the header within the result section */ .result-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; padding-bottom: 15px; border-bottom: 1px solid var(--border-color); } .result-header h3 { color: var(--primary-color); margin: 0; padding: 0; border: none; } /* NEW: Styles for the view toggle buttons */ .view-toggle button { padding: 8px 16px; border: 1px solid var(--border-color); background-color: transparent; cursor: pointer; font-size: 0.9rem; color: var(--light-text); transition: all 0.2s ease-in-out; } .view-toggle button:first-child { border-top-left-radius: var(--radius); border-bottom-left-radius: var(--radius); border-right: none; } .view-toggle button:last-child { border-top-right-radius: var(--radius); border-bottom-right-radius: var(--radius); } .view-toggle button.active { background-color: var(--primary-color); color: white; border-color: var(--primary-color); } .view-toggle button:hover:not(.active) { background-color: #f0f0f0; } /* NEW: Styles for the results table */ .table-container { overflow-x: auto; } .results-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .results-table th, .results-table td { padding: 12px 15px; text-align: left; border-bottom: 1px solid var(--border-color); } .results-table thead th { background-color: #f8f9fa; font-weight: 600; color: var(--text-color); } .results-table tbody tr:nth-child(even) { background-color: #f8f9fa; } .results-table tbody tr:hover { background-color: #f1f1f1; } .results-table td:first-child { font-weight: 500; color: var(--primary-dark); } .json-container { background-color: #282c34; border-radius: var(--radius); padding: 20px; margin-bottom: 15px; overflow-x: auto; } .json-highlight { font-size: 0.9rem; line-height: 1.5; } .copy-button { display: flex; align-items: center; gap: 8px; padding: 10px 15px; background-color: var(--primary-color); color: white; border: none; border-radius: var(--radius); font-size: 0.9rem; cursor: pointer; transition: background-color 0.3s ease; } .copy-button:hover { background-color: var(--primary-dark); } .app-footer { text-align: center; padding: 20px 0; color: var(--light-text); font-size: 0.9rem; } @media (max-width: 768px) { .app-container { padding: 10px; } .request-form, .result-section, .error-section { padding: 20px; } .status-message { flex-direction: column; gap: 10px; } .status-icon { width: 30px; height: 30px; } .result-header { flex-direction: column; align-items: flex-start; gap: 15px; } } </style>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.