Vue JSON Viewer Component
vue-json-views is a Vue component designed for efficiently displaying large JSON data structures. Currently at version 1.3.0, its primary differentiator is optimized rendering performance, claiming fast load times for JSON files up to 1MB, which addresses common slowdowns experienced with other JSON viewer components when dealing with extensive node counts. The library provides fundamental viewing capabilities with several configurable options, including initial collapse state, display depth, theme selection (one-dark, vs-code), icon styles, font size, and line height. While a specific release cadence isn't documented, its version history suggests active maintenance. It aims to offer a performant and customizable solution for integrating JSON visualization directly into Vue applications, contrasting with heavier alternatives or those tied to other frameworks.
Common errors
-
[Vue warn]: Failed to resolve component: json-view
cause The `json-view` component was used in the template but not properly registered with the Vue application or component.fixEnsure `jsonView` is imported and then listed in the `components` option of your Vue component or app instance: `import jsonView from 'vue-json-views'; export default { components: { jsonView } }`. -
TypeError: Cannot read properties of undefined (reading 'default') at <anonymous>
cause This error typically occurs in a browser environment when trying to access `window['vue-json-view'].default` if the script `build/index.js` (or CDN equivalent) hasn't fully loaded or the path is incorrect, or if you're trying to use `window['vue-json-view']` directly without the `.default` property for component registration.fixVerify the script for `vue-json-views` has loaded correctly in your HTML. When registering the component globally in a non-engineered project, use `jsonView: window['vue-json-view'].default` as shown in the documentation.
Warnings
- gotcha The `deep` prop controls the initial expansion depth. Setting it to a very large value can negate performance benefits, especially with huge JSON objects, as more nodes will be rendered initially. The documentation recommends not exceeding a value of `5`.
- gotcha When customizing `font-size` and `line-height` props, other related UI elements such as icons and padding will not automatically scale. This can lead to visual inconsistencies or layout issues if extreme values are chosen.
- gotcha The npm package name is `vue-json-views` (plural 'views'), but the component is commonly imported as `jsonView` (singular). Ensure correct package installation with `npm i -S vue-json-views` to avoid 'package not found' errors.
- gotcha The README suggests copying the uncompiled component directly into a project for customization. While this allows deep modifications, it disconnects the project from npm updates, potentially missing bug fixes, performance improvements, or new features from the official package.
Install
-
npm install vue-json-views -
yarn add vue-json-views -
pnpm add vue-json-views
Imports
- jsonView
const jsonView = require('vue-json-views')import jsonView from 'vue-json-views'
- jsonView (component registration)
export default { components: { jsonView } } - window['vue-json-view'].default
components: { jsonView: window['vue-json-view'] }components: { jsonView: window['vue-json-view'].default }
Quickstart
import { createApp } from 'vue';
import jsonView from 'vue-json-views';
const app = createApp({
components: {
jsonView
},
data() {
return {
// Example large JSON data for demonstration
json: {
id: 1,
name: 'Product A',
details: {
price: 99.99,
currency: 'USD',
features: [
{ key: 'material', value: 'plastic' },
{ key: 'weight', value: '100g' },
{ key: 'color', value: 'blue' }
],
reviews: Array(500).fill(null).map((_, i) => ({ user: `User${i}`, rating: Math.floor(Math.random() * 5) + 1, comment: `Good product review ${i}` }))
},
metadata: {
createdAt: '2023-01-01',
updatedAt: '2023-04-20'
},
relatedProducts: Array(10).fill(null).map((_, i) => ({ id: i + 2, name: `Product ${String.fromCharCode(66 + i)}` }))
}
};
},
template: `
<div style="height: 500px; overflow: auto; border: 1px solid #ccc; padding: 10px;">
<h1>JSON Data Viewer</h1>
<json-view
:data="json"
:closed="false"
:deep="3"
icon-style="triangle"
theme="one-dark"
:font-size="16"
/>
</div>
`
});
app.mount('#app');