A web-based tool to view, edit, format, repair, query, transform, and validate JSON
A web-based tool to view, edit, format, repair, query, transform, and validate JSON
A web-based tool to view, edit, format, transform, and validate JSON.
Try it out:
The library is written with Svelte, but can be used in plain JavaScript too and in any framework (SolidJS, React, Vue, Angular, etc.). It requires a browser from March 2022 or newer.
For usage in a Svelte project:
npm install svelte-jsoneditorNote that [email protected] requires Svelte 5. If you use Svelte 3 or 4, install [email protected].
For usage in vanilla JavaScript or frameworks like SolidJS, React, Vue, Angular, etc:
npm install vanilla-jsoneditorFor details on individual releases, see the changelog at https://github.com/josdejong/svelte-jsoneditor/releases.
Create a JSONEditor with two-way binding bind:json:
Or one-way binding:
The library provides a vanilla bundle of the editor via the npm library vanilla-jsoneditor (instead of svelte-jsoneditor) which can be used in any browser environment and framework. In a framework like React, Vue, or Angular, you'll need to write some wrapper code around the class interface.
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:
// for use in a React, Vue, or Angular project
import { JSONEditor } from 'vanilla-jsoneditor'If you want to use the library straight in the browser, use the provided standalone ES bundle:
// for use directly in the browser
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' instead of the standalone bundle so dependencies can be reused.
Browser example loading the standalone ES module:
JSONEditor
To make it easier to use the library in your framework of choice, you can use a wrapper library:
Svelte component:
JavasScript class:
import { createJSONEditor } from 'vanilla-jsoneditor' // or 'vanilla-jsoneditor/standalone.js'
const content = { text: '[1,2,3]' }
const editor = createJSONEditor({
target: document.getElementById('jsoneditor'),
props: {
content,
onChange: (updatedContent, previousContent, { contentErrors, patchResult }) => {
// content is an object { json: unknown } | { text: string }
console.log('onChange', { updatedContent, previousContent, contentErrors, patchResult })
}
}
})Properties such as content and mode are either passed as attributes to the Svelte component, like ``, or via the props in case of the vanilla JS factory function: createJSONEditor({ target, props: { content, mode }.
content: ContentPass the JSON contents to be rendered in the JSONEditor. Content is an object containing a property json (a parsed JSON document) or text (a stringified JSON document). Only one of the two properties must be defined. You can pass both content types to the editor independent of in what mode it is. You can use two-way binding via bind:content.
IMPORTANT: only make immutable changes to
content. Mutable changes will mess up history and rendered contents. See section Immutability.
selection: JSONEditorSelection | undefinedThe current selected contents. You can use two-way binding using bind:selection. The tree mode supports MultiSelection, KeySelection, EditKeySelection, ValueSelection, EditValueSelection, InsideSelection, or AfterSelection. The table mode supports ValueSelection, and text mode supports TextSelection..
mode: 'tree' | 'text' | 'table'Open the editor in 'tree' mode (default), 'table' mode, or 'text' mode (formerly: code mode).
ariaLabel: stringThe accessible name of the editor, applied to the contents of all three modes. Default value is 'JSON editor'. Give each editor a name of its own when a page contains more than one, so screen reader users can tell them apart.
mainMenuBar: booleanShow the main menu bar. Default value is true.
navigationBar: booleanShow the navigation bar with, where you can see the selected path and navigate through your document from there. Default value is true.
statusBar: booleanShow a status bar at the bottom of the 'text' editor, showing information about the cursor location and selected contents. Default value is true.
askToFormat: booleanWhen true (default), the user will be asked whether he/she wants to format the JSON document when a compact document is loaded or pasted in 'text' mode. Only applicable to 'text' mode.
readOnly: booleanOpen the editor in read-only mode: no changes can be made, non-relevant buttons are hidden from the menu, and the context menu is not enabled. Default value is false.
indentation: number | stringNumber of spaces use for indentation when stringifying JSON, or a string to be used as indentation like '\t' to use a tab as indentation, or ' ' to use 4 spaces (which is equivalent to configuring indentation: 4). See also property tabSize.
tabSize: numberWhen indentation is configured as a tab character (indentation: '\t'), tabSize configures how large a tab character is rendered. Default value is 4. Only applicable to text mode.
truncateTextSize: numberJSON values containing a string with a length greater than truncateTextSize bytes will be truncated in tree mode and table mode. The text can be expanded by clicking the button "Show more" at the end of the string. The default value is 1000 bytes.
escapeControlCharacters: booleanFalse by default. When true, control characters like newline and tab are rendered as escaped characters \n and \t. Only applicable for 'tree' mode, in 'text' mode control characters are always escaped.
escapeUnicodeCharacters: booleanFalse by default. When true, unicode characters like ☎ and are rendered escaped like \u260e and \ud83d\ude00.
flattenColumns: booleanTrue by default. Only applicable to 'table' mode. When true, nested object properties will be displayed each in their own column, with the nested path as column name. When false, nested objects will be rendered inline, and double-clicking them will open them in a popup.
validator: function (json: unknown): ValidationError[]Validate the JSON document.
You can use the built-in JSON Schema validator powered by Ajv:
import { createAjvValidator } from 'svelte-jsoneditor'
const validator = createAjvValidator({ schema, schemaDefinitions })If you want Ajv to load remote schemas through its loadSchema function, use the async validator builder like this:
import { createAjvValidatorAsync } from 'svelte-jsoneditor'
const validator = await createAjvValidatorAsync({
schema: {
$ref: "/schema.json",
},
ajvOptions: {
loadSchema: function (uri: string) {
return fetch(uri).then((response) => response.json());
},
},
});createAjvValidatorAsync resolves only after Ajv has loaded and compiled all referenced schemas, or rejects on errors, e.g. if loadSchema returns a rejected promise.
parser: JSON = JSONConfigure a custom JSON parser, like lossless-json. By default, the native JSON parser of JavaScript is used. The JSON interface is an object with a parse and stringify function. For example:
validationParser: JSONParser = JSONOnly applicable when a validator is provided. This is the same as parser, except that this parser is used to parse the data before sending it to the validator. Configure a custom JSON parser that is used to parse JSON before passing it to the validator. By default, the built-in JSON parser is used. When passing a custom validationParser, make sure the output of the parser is supported by the configured validator. So, when the validationParser can output bigint numbers or other numeric types, the validator must also support that. In tree mode, when parser is not equal to validationParser, the JSON document will be converted before it is passed to the validator via validationParser.parse(parser.stringify(json)).
pathParser: JSONPathParserAn optional object with a parse and stringify method to parse and stringify a JSONPath, which is an array with property names. The pathParser is used in the path editor in the navigation bar, which is opened by clicking the edit button on the right side of the navigation bar. The pathParser.parse function is allowed to throw an Error when the input is invalid. By default, a JSON Path notation is used, which looks like $.data[2].nested.property. Alternatively, it is possible to use for example a JSON Pointer notation like /data/2/nested/property or something custom-made. Related helper functions: parseJSONPath and stringifyJSONPath, parseJSONPointer and compileJSONPointer.
maxDocumentSizeTextMode: numberIn text mode, JSON documents with a total length greater than maxDocumentSizeTextMode will not be shown. Instead, a warning will be displayed indicating that the browser may crash if it attempts to load the document. The user can then choose to override the warning and open the document anyway, open the document in the lighter tree mode instead, or cancel.
No open issues yet, or sync has not completed.