JSON Editor Vue

0.18.1 · active · verified Sun Apr 19

json-editor-vue is an isomorphic Vue component that functions as a comprehensive JSON editor, viewer, formatter, and validator. It leverages the underlying `vanilla-jsoneditor` library to provide its core functionality. The package is actively maintained, with the current stable version being v0.18.1, reflecting a frequent update schedule that includes both bug fixes and feature enhancements, such as regular upgrades to its `vanilla-jsoneditor` dependency. Key differentiators include its robust support for Server-Side Rendering (SSR) in Nuxt environments, comprehensive validation capabilities (indicated by AJV keywords), and broad compatibility across Vue 2 and Vue 3 versions. It offers multiple editing modes (text, tree, table) and handles large JSON documents efficiently, partly due to its use of `destr` for deserialization.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `json-editor-vue` into a Vue 3 component using the Composition API. It showcases data binding with `v-model`, setting the editor mode, enabling UI elements like the menu and navigation bar, and handling `update:json` and `error` events for robust interaction. Types for `Mode` and `JSONContent` are imported from `vanilla-jsoneditor` for type safety.

<!-- MyJsonEditorComponent.vue -->
<template>
  <div class="json-editor-container">
    <h2>Edit Your JSON Data</h2>
    <p>Current mode: {{ mode }}</p>
    <JsonEditorVue
      v-model="jsonData"
      :mode="mode"
      :mainMenuBar="true"
      :navigationBar="true"
      :statusBar="true"
      :indentation="2"
      @update:json="handleJsonUpdate"
      @error="handleError"
      class="my-editor"
    />
    <h3>Raw JSON Output:</h3>
    <pre class="json-output">{{ JSON.stringify(jsonData, null, 2) }}</pre>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import JsonEditorVue from 'json-editor-vue';
import type { Mode, JSONContent } from 'vanilla-jsoneditor';

const initialData: object | null = {
  "user": {
    "id": 123,
    "name": "Alice",
    "email": "alice@example.com",
    "settings": {
      "theme": "dark",
      "notifications": true,
      "language": "en-US"
    },
    "tags": ["admin", "premium", "active"],
    "lastLogin": "2024-04-18T10:00:00Z"
  },
  "products": [
    { "productId": "A1", "price": 29.99, "inStock": true },
    { "productId": "B2", "price": 12.50, "inStock": false }
  ]
};

const jsonData = ref<object | null>(initialData);
const mode = ref<Mode>('tree'); // 'tree', 'text', 'form', 'code', 'preview' options available

function handleJsonUpdate(newValue: object | null) {
  console.log('JSON content updated:', newValue);
  // You can perform further actions with the updated JSON here
}

function handleError(error: Error) {
  console.error('JSON Editor encountered an error:', error.message);
  // Display error to user or log for debugging
}
</script>

<style>
.json-editor-container {
  font-family: Arial, sans-serif;
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

h2 {
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.my-editor {
  height: 500px; /* Essential for the editor to display */
  width: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-bottom: 20px;
}

.json-output {
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  padding: 10px;
  border-radius: 4px;
  white-space: pre-wrap;
  word-break: break-all;
}
</style>

view raw JSON →