vanilla-jsoneditor
A web-based tool to view, edit, format, transform, and validate JSON.
Try it out: https://jsoneditoronline.org
This is the vanilla variant of svelte-jsoneditor
, which can be used in vanilla JavaScript or frameworks like SolidJS, React, Vue, Angular.
Features
- View and edit JSON
- Has a low level text editor and high level tree view and table view
- Format (beautify) and compact JSON
- Sort, query, filter, and transform JSON
- Repair JSON
- JSON schema validation and pluggable custom validation
- Color highlighting, undo/redo, search and replace
- Utilities like a color picker and timestamp tag
- Handles large JSON documents up to 512 MB
Install
Install using npm:
npm install vanilla-jsoneditor
Remark: for usage in a Svelte project, install and use svelte-jsoneditor
instead of vanilla-jsoneditor
.
Use
If you have a setup for your project with a bundler (like Vite, Rollup, or Webpack), it is best to use the default ES import:
import { createJSONEditor } from 'vanilla-jsoneditor'
If you want to use the library straight in the browser, use the provided standalone ES bundle:
import { createJSONEditor } from 'vanilla-jsoneditor/standalone.js'
The standalone bundle contains all dependencies of vanilla-jsoneditor
, for example lodash-es
and Ajv
. If you use some of these dependencies in your project too, it means that they will be bundled twice in your web application, leading to a needlessly large application size. In general, it is preferable to use the default import { createJSONEditor } from 'vanilla-jsoneditor'
so dependencies can be reused.
Use (Browser example loading the ES module)
<!doctype html>
<html lang="en">
<head>
<title>JSONEditor</title>
</head>
<body>
<div id="jsoneditor"></div>
<script type="module">
import { createJSONEditor } from 'vanilla-jsoneditor/standalone.js'
let content = {
text: undefined,
json: {
greeting: 'Hello World'
}
}
const editor = createJSONEditor({
target: document.getElementById('jsoneditor'),
props: {
content,
onChange: (updatedContent, previousContent, { contentErrors, patchResult }) => {
console.log('onChange', { updatedContent, previousContent, contentErrors, patchResult })
content = updatedContent
}
}
})
</script>
</body>
</html>
Use (React example, including NextJS)
First, create a React component to wrap the vanilla-jsoneditor
Depending on whether you are using JavaScript of TypeScript, create either a JSX or TSX file:
TypeScript
import { useEffect, useRef } from 'react'
import { createJSONEditor, JSONEditorPropsOptional } from 'vanilla-jsoneditor'
const JSONEditorReact: React.FC<JSONEditorPropsOptional> = (props) => {
const refContainer = useRef<HTMLDivElement>(null)
const refEditor = useRef<JSONEditor | null>(null)
useEffect(() => {
refEditor.current = createJSONEditor({
target: refContainer.current!,
props: {}
})
return () => {
if (refEditor.current) {
refEditor.current.destroy()
refEditor.current = null
}
}
}, [])
useEffect(() => {
if (refEditor.current) {
refEditor.current.updateProps(props)
}
}, [props])
return <div ref={refContainer}></div>
}
export default JSONEditorReact
JavaScript
import { useEffect, useRef } from 'react'
import { JSONEditor, JSONEditorPropsOptional } from 'vanilla-jsoneditor'
const JSONEditorReact = (props) => {
const refContainer = useRef(null)
const refEditor = useRef(null)
useEffect(() => {
refEditor.current = createJSONEditor({
target: refContainer.current,
props: {}
})
return () => {
if (refEditor.current) {
refEditor.current.destroy()
refEditor.current = null
}
}
}, [])
useEffect(() => {
if (refEditor.current) {
refEditor.current.updateProps(props)
}
}, [props])
return <div ref={refContainer}></div>
}
export default JSONEditorReact
Import and use the React component
If you are using NextJS, you will need to use a dynamic import to only render the component in the browser (disabling server-side rendering of the wrapper), as shown below in a NextJS TypeScript example.
If you are using React in an conventional non-NextJS browser app, you can import the component using a standard import statement like import JSONEditorReact from '../JSONEditorReact'
import dynamic from 'next/dynamic'
import { useCallback, useState } from 'react'
import type { Content, OnChangeStatus } from 'vanilla-jsoneditor'
const JSONEditorReact = dynamic(() => import('../JSONEditorReact'), { ssr: false })
const TextContent = dynamic(() => import('../TextContent'), { ssr: false })
const initialContent = {
hello: 'world',
count: 1,
foo: ['bar', 'car']
}
export default function Demo() {
const [jsonContent, setJsonContent] = useState<Content>({ json: initialContent })
const handler = useCallback(
(content: Content, previousContent: Content, status: OnChangeStatus) => {
setJsonContent(content)
},
[jsonContent]
)
return (
<div>
<JSONEditorReact content={jsonContent} onChange={handler} />
<TextContent content={jsonContent} />
</div>
)
}
import { Content, toTextContent } from 'vanilla-jsoneditor'
interface IOwnProps {
content: Content
}
const TextContent = (props: IOwnProps) => {
const { content } = props
return (
<p>
The contents of the editor, converted to a text string, are: {toTextContent(content).text}
</p>
)
}
export default TextContent