- Notifications
You must be signed in to change notification settings - Fork693
json-editor/json-editor
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Fork of the inactivejdorn/json-editor using the updated forkjson-editor/json-editor.Some pull requests added from the original repo.
JSON Editor takes a JSON Schema and uses it to generate an HTML form.It has full support for JSON Schema version 3 and 4 and can integrate with several popular CSS frameworks (bootstrap, spectre, tailwind).
Take a look at thisexample for a simple form submission case study.
Check out an interactive demo:https://json-editor.github.io/json-editor/
Or the JSON-Editor Interactive Playground:https://pmk65.github.io/jedemov2/dist/demo.html
Install package
npm install @json-editor/json-editor
Using a CDN
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
You can also access older releases from CDN, using the landing page:https://www.jsdelivr.com/package/npm/@json-editor/json-editor
For local usage download theproduction version or thedevelopment version
JSON Editor has no dependencies. It only needs a modern browser (tested in Chrome and Firefox).
The following are not required, but can improve the style and usability of JSON Editor when present.
- A compatible JS template engine (Mustache, Underscore, Hogan, Handlebars, Lodash, Swig, Markup, or EJS)
- A compatible CSS framework for styling (Spectre, Tailwind, Bootstrap4)
- A compatible icon library (Spectre, jQueryUI, Font Awesome 3/4/5)
- SCEditor for WYSIWYG editing of HTML or BBCode content
- SimpleMDE for editing of Markdown content
- Ace Editor for editing code
- Jodit Open Source WYSIWYG editor
- Autocomplete Accessible autocomplete component
- Choices for nicer Select & Array boxes
- Select2 for nicer Select boxes
- Selectize for nicer Select & Array boxes
- Flatpickr lightweight and powerful datetime picker
- Signature Pad HTML5 canvas based smooth signature drawing
- Vanilla Picker A simple, easy to use vanilla JS color picker with alpha selection
- Cleave.js for formatting your<input/> content while you are typing
- IMask.js vanilla javascript input mask
- math.js for more accurate floating point math (multipleOf, divisibleBy, etc.)
- DOMPurify DOM-only, super-fast, uber-tolerant XSS sanitizer. (If you want to use HTML format in titles/headers and descriptions.)
If you learn best by example, check these out:
- Basic Usage Example -https://json-editor.github.io/json-editor/basic.html
- ACE Editor Example -https://json-editor.github.io/json-editor/ace_editor.html
- Advanced Usage Example -https://json-editor.github.io/json-editor/advanced.html
- CSS Integration Example -https://json-editor.github.io/json-editor/css_integration.html
- Base64 Editor Example (Muiltple Upload) -https://json-editor.github.io/json-editor/multiple_upload_base64.html
- Choices Editor Example -https://json-editor.github.io/json-editor/choices.html
- Cleave.js Editor Example -https://json-editor.github.io/json-editor/cleave.html
- Colorpicker Editor Example -https://json-editor.github.io/json-editor/colorpicker.html
- Datetime Editor Example -https://json-editor.github.io/json-editor/datetime.html
- DescribedBy Hyperlink Editor Example -https://json-editor.github.io/json-editor/describedby.html
- iMask.js Editor Example -https://json-editor.github.io/json-editor/imask.html
- Radio Button JSON Editor Example -https://json-editor.github.io/json-editor/radio.html
- Recursive JSON Editor Example -https://json-editor.github.io/json-editor/recursive.html
- Select2 Editor Example -https://json-editor.github.io/json-editor/select2.html
- Selectize Editor Example -https://json-editor.github.io/json-editor/selectize.html
- Signature Pad Editor Example -https://json-editor.github.io/json-editor/signature.html
- Star Rating Editor Example -https://json-editor.github.io/json-editor/starrating.html
- Upload Editor Example -https://json-editor.github.io/json-editor/upload.html
- WYSIWYG Editor Example -https://json-editor.github.io/json-editor/wysiwyg.html
- Meta schema (schema builder) Example -https://json-editor.github.io/json-editor/meta-schema.html
More examples can be found at theJSON-Editor Interactive Playground
The rest of this README contains detailed documentation about every aspect of JSON Editor. For moreunder-the-hood documentation, check the wiki.
constelement=document.getElementById('editor_holder');consteditor=newJSONEditor(element,options);
Options can be set globally or on a per-instance basis during instantiation.
// Set an option globallyJSONEditor.defaults.options.theme='bootstrap4';// Set an option during instantiationconsteditor=newJSONEditor(element,{//...theme:'bootstrap4'});
Here are all the available options:
Option | Description | Default Value |
---|---|---|
ajax | Iftrue , JSON Editor will load external URLs in$ref via ajax. | false |
ajaxBase | Allows schema references to work either with or without cors; set to protocol://host:port when api is served by different host. |
|
ajaxCredentials | Iftrue , JSON Editor will make ajax call with [credentials](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials). | false |
ajax_cache_responses | Iftrue , JSON Editor will cache external URLs' schemas inlocalStorage to avoid subsequent ajax calls. | false |
ajax_cache_buster | Ifajax_cache_responses is enabled, use this string to invalidate stale caches. E.g., this value should be changed when schemas are updated. | Current date in simplied ISO-8601 format (e.g., "2011-10-05" for October 5, 2011). |
compact | Iftrue , the label will not be displayed/added. | false |
disable_array_add | Iftrue , remove all "add row" buttons from arrays. | false |
disable_array_delete | Iftrue , remove all "delete row" buttons from arrays. | false |
disable_array_delete_all_rows | Iftrue , remove all "delete all rows" buttons from arrays. | false |
disable_array_delete_last_row | Iftrue , remove all "delete last row" buttons from arrays. | false |
disable_array_reorder | Iftrue , remove all "move up" and "move down" buttons from arrays. | false |
enable_array_copy | Iftrue , add copy buttons to arrays. | false |
disable_collapse | Iftrue , remove all collapse buttons from objects and arrays. | false |
disable_edit_json | Iftrue , remove all Edit JSON buttons from objects. | false |
disable_properties | Iftrue , remove all Edit Properties buttons from objects. | false |
array_controls_top | Iftrue , array controls (add, delete etc) will be displayed at top of list. | false |
form_name_root | The first part of the `name` attribute of form inputs in the editor. An full example name is `root[person][name]` where "root" is the form_name_root. | root |
iconlib | The icon library to use for the editor. See theCSS Integration section below for more info. | null |
remove_button_labels | Display only icons in buttons. This works only if iconlib is set. | false |
no_additional_properties | Iftrue , objects can only contain properties defined with theproperties keyword unless the propertyadditionalProperties: true is specified in the object schema | false |
refs | An object containing schema definitions for URLs. Allows you to pre-define external schemas. | {} |
required_by_default | Iftrue , all schemas that don't explicitly set therequired property will be required. | false |
keep_oneof_values | Iftrue , makes oneOf copy properties over when switching. | true |
keep_only_existing_values | Iftrue , copy only existing properties over when switching. | false |
schema | A valid JSON Schema to use for the editor. Version 3 and Version 4 of the draft specification are supported. | {} |
show_errors | When to show validation errors in the UI. Valid values areinteraction ,change ,always , andnever . | "interaction" |
startval | Seed the editor with an initial value. This should be valid against the editor's schema. | null |
template | The JS template engine to use. See theTemplates and Variables section below for more info. | default |
theme | The CSS theme to use. See theCSS Integration section below for more info. | html |
display_required_only | Iftrue , only required properties will be included by default. | false |
show_opt_in | Iftrue , NON required properties will have an extra toggable checkbox near the title that determines if the value must be included or not in the editor´s value | false |
prompt_before_delete | Iftrue , displays a dialog box with a confirmation message before node deletion. | true |
object_layout | The default value of `format` for objects. If set totable for example, objects will use table layout if `format` is not specified. | normal |
enum_source_value_auto_select | Preserve value at Move Up or Down.(No value is selected automatically upon deletion.) | true |
max_depth | Max depth of the nested properties to be rendered of provided json schema. The missing of this option could cause "maximum call stack size exceeded" in case of object properties with circular references.0 value means "render all". | 0 |
use_default_values | If true default values based on the "type" of the property will be used | true |
urn_resolver | A callback function to resolve an undefined Uniform Resource Name (URN) for$ref . The function receives a URN and callback to pass back a serialized JSON response. The function should return a boolean (true if the URN can be resolved; false otherwise). | false |
use_name_attributes | Iftrue , control inputsname attributes will be set. | true |
button_state_mode | If1 , inactive buttons are hidden. If2 , inactive buttons are disabled. | 1 |
case_sensitive_property_search | This property controls whether property searches in an object editor are case-sensitive | true |
prompt_paste_max_length_reached | Iftrue , an alert will be displayed when pasting a value in an input that exceeded maxLength | false |
enforce_const | When set totrue , the schemaconst will serve as the default value, and the editor will prevent any changes to it. | false |
opt_in_widget | Which widget should be used for opt-in inputs. Possible values are'checkbox' and'switch' | 'checkbox' |
*Note If theajax
property istrue
and JSON Editor needs to fetch an external url, the api methods won't be available immediately.Listen for theready
event before calling them.
editor.on('ready',()=>{// Now the api methods will be availableeditor.validate();});
editor.setValue({name:"John Smith"});constvalue=editor.getValue();console.log(value.name)// Will log "John Smith"
Instead of getting/setting the value of the entire editor, you can also work on individual parts of the schema:
// Get a reference to a node within the editorconstname=editor.getEditor('root.name');// `getEditor` will return null if the path is invalidif(name){name.setValue("John Smith");console.log(name.getValue());}
When feasible, JSON Editor won't let users enter invalid data. This is done byusing input masks and intelligently enabling/disabling controls.
However, in some cases it is still possible to enter data that doesn't validate against the schema.
You can use thevalidate
method to check if the data is valid or not.
// Validate the editor's current value against the schemaconsterrors=editor.validate();if(errors.length){// errors is an array of objects, each with a `path`, `property`, and `message` parameter// `property` is the schema keyword that triggered the validation error (e.g. "minLength")// `path` is a dot separated path into the JSON object (e.g. "root.path.to.field")console.log(errors);}else{// It's valid!}
By default, this will do the validation with the editor's current value.If you want to use a different value, you can pass it in as a parameter.
// Validate an arbitrary value against the editor's schemaconsterrors=editor.validate({value:{to:"test"}});
Display validation errors on demand
editor.showValidationErrors();
Thechange
event is fired whenever the editor's value changes.
editor.on('change',()=>{// Do something});editor.off('change',function_reference);
You can also watch a specific field for changes:
editor.watch('path.to.field',()=>{// Do something});editor.unwatch('path.to.field',function_reference);
Or watch all fields (Similar to the "onchange" event, but tracks the field changed)
constwatcherCallback=function(path){console.log(`field with path: [${path}] changed to [${JSON.stringify(this.getEditor(path).getValue())}]`);// Do something}for(letkeyineditor.editors){if(editor.editors.hasOwnProperty(key)&&key!=='root'){editor.watch(key,watcherCallback.bind(editor,key));}}
There are alsoadd
andswitch
events to track changes.Theadd
event fires when a new object property has just been added.
editor.on('add',(property)=>{// Do something});
Theswitch
event fires when the type of one of the object's properties is changed by a type switch on the form.
editor.on('switch',(property)=>{// Do something});
This lets you disable editing for the entire form or part of the form.
// Disable entire formeditor.disable();// Disable part of the formeditor.getEditor('root.location').disable();// Enable entire formeditor.enable();// Enable part of the formeditor.getEditor('root.location').enable();// Check if form is currently enabledif(editor.isEnabled())alert("It's editable!");// Activate part of the formeditor.activate();// Deactivate part of the formeditor.deactivate();
This removes the editor HTML from the DOM and frees up resources.
editor.destroy();
JSON Editor can integrate with several popular CSS frameworks out of the box.
The currently supported themes are:
- barebones
- html (the default)
- bootstrap3
- bootstrap4
- bootstrap5
- spectre
- tailwind
Note: The following themes have NOT been updated to 2.x format and will be removed in final version unless there's someone willing to update those.Old 1.x themes displays the message"This is an old JSON-Editor 1.x Theme and might not display elements correctly when used with the 2.x version" at the bottom of the form output.
- bootstrap2
- foundation3
- foundation4
- foundation5
- foundation6
- jqueryui
- materialize
The default theme ishtml
, which does not rely on an external framework.This default can be changed by setting theJSONEditor.defaults.options.theme
variable.
If you want to specify your own styles with CSS, you can usebarebones
, which includes almost no classes or inline styles.
JSONEditor.defaults.options.theme='spectre';
You can override this default on a per-instance basis by passing atheme
parameter in when initializing:
consteditor=newJSONEditor(element,{schema:schema,theme:'tailwind'});
JSON Editor also supports several popular icon libraries. The icon library must be set independently of the theme, even though there is some overlap.
The supported icon libs are:
- jqueryui
- fontawesome3
- fontawesome4
- fontawesome5
- openiconic
- spectre
- bootstrap
By default, no icons are used. Just like the CSS theme, you can set the icon lib globally or when initializing:
// Set the global defaultJSONEditor.defaults.options.iconlib="spectre";// Set the icon lib during initializationconsteditor=newJSONEditor(element,{schema:schema,iconlib:"fontawesome4"});
It's possible to create your own custom themes and/or icon libs as well. Look at any of the existing classes for examples.
JSON Editor fully supports version 3 and 4 of the JSON Schemacore andvalidation specifications.Some of Thehyper-schema specification is supported as well.
JSON Editor supports schema references to external URLs and local definitions as well as JSON Pointers. Here's an example:
{"type":"object","properties": {"name": {"title":"Full Name","$ref":"#/definitions/name" },"location": {"$ref":"http://mydomain.com/geo.json" },"birthday": {"$ref":"http://mydomain.com/person.json#/definitions/birthdate" } },"definitions": {"name": {"type":"string","minLength":5 } }}
Local references must point to thedefinitions
object of the root node of the schema.So,#/customkey/name
will throw an exception.
If loading an external url via Ajax, the url must either be on the same domain or return the correct HTTP cross domain headers.If your URLs don't meet this requirement, you can pass in the references to JSON Editor during initialization (see Usage section above).
Self-referential $refs are supported. Check outexamples/recursive.html
for usage examples.
Thelinks
keyword from the hyper-schema specification can be used to add links to related documents.
JSON Editor will use themediaType
property of the links to determine how best to display them.Image, audio, and video links will display the media inline as well as providing a text link.
Here are a couple examples:
Simple text link
{ "title": "Blog Post Id", "type": "integer", "links": [ { "rel": "comments", "href": "/posts/{{self}}/comments/", // Optional - set CSS classes for the link "class": "comment-link open-in-modal primary-text" } ]}
Make link download when clicked
{ "title": "Document filename", "type": "string", "links": [ { "rel": "Download File", "href": "/documents/{{self}}", // Can also set `download` to a string as per the HTML5 spec "download": true } ]}
Show a video preview (using HTML5 video)
{ "title": "Video filename", "type": "string", "links": [ { "href": "/videos/{{self}}.mp4", "mediaType": "video/mp4" } ]}
Thehref
property is a template that gets re-evaluated every time the value changes.The variableself
is always available. Look at theDependencies section below for how to include other fields or use a custom template engine.
The if-then-else keywords are used to express conditional validation logic based on the evaluation of a specified condition. The if keyword defines a condition, and depending on whether it evaluates to true or false, the schema specified under either the then or else keywords will be applied.
{"type":"object","properties": {"street_address": {"type":"string" },"country": {"type":"string","default":"United States of America","enum": ["United States of America","Canada" ] },"postal_code": {"type":"string" } },"if": {"properties": {"country": {"const":"United States of America" } } },"then": {"properties": {"postal_code": {"pattern":"[0-9]{5}(-[0-9]{4})?" } } },"else": {"properties": {"postal_code": {"pattern":"[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } } }}
There is no way to specify property ordering in JSON Schema (although this may change in v5 of the spec).
JSON Editor introduces a new keywordpropertyOrder
for this purpose. The default property order if unspecified is 1000. Properties with the same order will use normal JSON key ordering.
{"type":"object","properties": {"prop1": {"type":"string" },"prop2": {"type":"string","propertyOrder":10 },"prop3": {"type":"string","propertyOrder":1001 },"prop4": {"type":"string","propertyOrder":1 } }}
In the above example schema,prop1
does not have an order specified, so it will default to 1000.So, the final order of properties in the form (and in returned JSON data) will be:
- prop4 (order 1)
- prop2 (order 10)
- prop1 (order 1000)
- prop3 (order 1001)
The default behavior of JSON Editor is to include all object properties defined with theproperties
keyword.
To override this behaviour, you can use the keyworddefaultProperties
to set which ones are included:
{"type":"object","properties": {"name": {"type":"string"},"age": {"type":"integer"} },"defaultProperties": ["name"]}
Now, only thename
property above will be included by default. You can use the "Object Properties" buttonto add the "age" property back in.
JSON Editor supports many different formats for schemas of typestring
. They will work with schemas of typeinteger
andnumber
as well, but some formats may produce weird results.If theenum
property is specified,format
will be ignored.
JSON Editor uses HTML5 input types, so some of these may render as basic text input in older browsers:
- color
- date
- datetime-local
- month
- password
- number
- range
- tel
- text
- textarea
- time
- url
- week
Here is an example that will show a color picker in browsers that support it:
{"type":"object","properties": {"color": {"type":"string","format":"color" } }}
You can set custom attributes such asplaceholder,class anddata- on the input field and on the editor containerusing the special options keywordinputAttributes
andcontainerAttributes
.
Like this:
{"type":"object","properties": {"name": {"title":"Full Name","options": {"inputAttributes": {"placeholder":"your name here...","class":"myclass" },"containerAttributes": {"data-container":"my-container","class":"my-container-class" } } } }}
In addition to the standard HTML input formats, JSON Editor can also integrate with several 3rd party specialized editors. These libraries are not included in JSON Editor and you must load them on the page yourself.
SCEditor provides WYSIWYG editing of HTML and BBCode. To use it, set the format toxhtml
orbbcode
and set thewysiwyg
option totrue
:
{"type":"string","format":"xhtml","options": {"wysiwyg":true }}
SimpleMDE is a simple Markdown editor with live preview. To use it, set the format tomarkdown
:
{"type":"string","format":"markdown"}
To customize the editor, add theconfiguration in thesimplemde
option:
{"type":"string","format":"markdown","options": {"simplemde": {"toolbar": ["bold","italic","heading","|","link","quote","|","preview","fullscreen","guide" ],"spellChecker":false } }}
Ace Editor is a syntax highlighting source code editor. You can use it by setting the format to any of the following:
- actionscript
- batchfile
- c
- c++
- cpp (alias for c++)
- coffee
- csharp
- css
- dart
- django
- ejs
- erlang
- golang
- groovy
- handlebars
- haskell
- haxe
- html
- ini
- jade
- java
- javascript
- json
- less
- lisp
- lua
- makefile
- markdown
- matlab
- mysql
- objectivec
- pascal
- perl
- pgsql
- php
- python
- prql
- r
- ruby
- rust
- sass
- scala
- scss
- smarty
- sql
- stylus
- svg
- typescript
- twig
- vbscript
- xml
- yaml
- zig
{"type":"string","format":"yaml"}
You can use the hyper-schema keywordmedia
instead offormat
too if you prefer for formats with a mime type:
{"type":"string","media": {"type":"text/html" }}
You can enableAce editor options individually by setting theoptions.ace
in schema.
{"type":"string","format":"sql","options": {"ace": {"theme":"ace/theme/vibrant_ink","tabSize":2,"useSoftTabs":true,"wrap":true } }}
JSONEditor features specialized types of editors.
Creates a button whose click callback can be defined inJSONEditor.defaults.callbacks
. Options:
icon
: Renders an icon into the button. Must be supported by the iconLib used.validated
: Withtrue
the button is disabled until the whole editor is valid.action
: the name of the callback that will be invoked when the button is clicked.
WarningFor the form to render properly all callbacks used in
action
s must also be defined inJSONEditor.defaults.callbacks
.
JSONEditor.defaults.callbacks={"button" :{"myAction" :function(jseditor,e){alert('Button action')}}}
{"format":"button","options": {"button": {"text":"Search","icon":"search","action":"myAction","validated":true } }}
Displays a label and a description text.
{"format":"info","title":"Important:","description":"Lorem ipsum dolor"}
The default boolean editor is a select box with options "true" and "false". To use a checkbox instead, set the format tocheckbox
.
{"type":"boolean","format":"checkbox"}
The default array editor takes up a lot of screen real estate. Thetable
andtabs
formats provide more compact UIs for editing arrays.
Thetable
format works great when every array element has the same schema and is not too complex.
Thetabs
format can handle any array, but only shows one array element at a time. It has tabs on the left for switching between items.
Thetabs-top
format place tabs on the top.
Here's an example of thetable
format:
{"type":"array","format":"table","items": {"type":"object","properties": {"name": {"type":"string" } } }}
For arrays of enumerated strings, you can also use theselect
orcheckbox
format. These formats require a very specific schema to work:
{"type":"array","uniqueItems":true,"items": {"type":"string","enum": ["value1","value2"] }}
By default, thecheckbox
editor (multiple checkboxes) will be used if there are fewer than 8 enum options. Otherwise, theselect
editor (a multiselect box) will be used.
You can override this default by passing in a format:
{"type":"array","format":"select","uniqueItems":true,"items": {"type":"string","enum": ["value1","value2"] }}
When an array item is added, removed, moved up, moved or removed the json editor will trigger a relative event.
editor.on('moveRow',editor=>{console.log('moveRow',editor)});editor.on('addRow',editor=>{console.log('addRow',editor)});editor.on('deleteRow',deletedValue=>{console.log('deleteRow',deletedValue)});editor.on('deleteAllRows',deletedValues=>{console.log('deleteAllRows',deletedValues)});
Drag and drop for array item is supported to perform a quick item moving.For array editor with format=tabs
ortabs-top
, you can drag the tab header directly.For the default array editor (or format=table
), you have to drag the array item panel with Ctrl key pressed (to avoid side-effect of draggable panel), or drag any text in the input box to another item panel.
When schemas are loaded via a request, theschemaLoaded
event is triggered individually for each schema after its loading.Once the loading of all schemas is completed, theallSchemasLoaded
event is triggered.
editor.on('schemaLoaded',(payload)=>{console.log('schemasLoaded',payload.schemaUrl)console.log('schemasLoaded',payload.schema)})editor.on('allSchemasLoaded',()=>{console.log('allSchemasLoaded')})
The default object layout is one child editor per row. Thegrid
format will instead put multiple child editors per row.This can make the editor much more compact, but at a cost of not guaranteeing child editor order. This format will stretchcolumns to fill gaps untill all the 12 columns are filled.
{"type":"object","properties": {"name": {"type":"string" } },"format":"grid"}
Thegrid-strict
format instead will respect columns sizes (no stretching) and properties order.It introduces the newgrid-break
property to breaks the current row leaving a 4 colums gap.
{"type":"object","format":"grid-strict","properties": {"a": {"title":"a","type":"string","options": {"grid_columns":4 } },"b": {"title":"b","type":"string","options": {"grid_columns":4,"grid_break":true } },"c": {"title":"c","type":"string","options": {"grid_columns":6 } },"d": {"title":"d","type":"string","options": {"grid_columns":6 } } }}
Thecategories
format groups properties in top-tabbed panels, one for each object or array property plus one that groups all added or other types of properties.Panel tabs titles came from object or array titles and for the grouping panel it defaults to "Basic", unlessbasicCategoryTitle
is defined.
{"type":"object","properties": {"name": {"type":"string" } },"format":"categories","basicCategoryTitle":"Main"}
Demo page will look like this:
Editors can accept options which alter the behavior in some way.
switcher
- If set to false and using oneOf or anyOf, a multiple editor is not created, but validation still applies. Should be used with schemas with same type.titleHidden
- If set to true, the editor title will be visually hiddencollapsed
- If set to true, the editor will start collapsed (works for objects and arrays)disable_array_add
- If set to true, the "add row" button will be hidden (works for arrays)disable_array_delete
- If set to true, all of the "delete" buttons will be hidden (works for arrays)disable_array_delete_all_rows
- If set to true, just the "delete all rows" button will be hidden (works for arrays)disable_array_delete_last_row
- If set to true, just the "delete last row" buttons will be hidden (works for arrays)disable_array_reorder
- If set to true, the "move up/down" buttons will be hidden (works for arrays)disable_collapse
- If set to true, the collapse button will be hidden (works for objects and arrays)disable_edit_json
- If set to true, the Edit JSON button will be hidden (works for objects)disable_properties
- If set to true, the Edit Properties button will be hidden (works for objects)array_controls_top
- If set to true, array controls (add, delete etc) will be displayed at top of list (works for arrays)enum
- SeeEnum optionsenum_titles
- An array of display values to use for select box options in the same order as defined with theenum
keyword. Works with schema using enum values.expand_height
- If set to true, the input will auto expand/contract to fit the content. Works best with textareas.grid_columns
- Explicitly set the number of grid columns (1-12) for the editor if it's within an object using a grid layout.hidden
- If set to true, the editor will not appear in the UI (works for all types)input_height
- Explicitly set the height of the input element. Should be a valid CSS width string (e.g. "100px"). Works best with textareas.input_width
- Explicitly set the width of the input element. Should be a valid CSS width string (e.g. "100px"). Works for string, number, and integer data types.remove_empty_properties
- If set totrue
for an object, empty object properties (i.e. those with falsy values) will not be returned by getValue().remove_false_properties
- If set totrue
for an object, object properties with valuefalse
will not be returned by getValue().has_placeholder_option
- If set to true, a placeholder option will be added to the select editor input.placeholder_option_text
- Text displayed in select placeholder option.
{"type":"object","options": {"collapsed":true },"properties": {"name": {"type":"string" } }}
You can globally set the default options too if you want:
JSONEditor.defaults.editors.object.options.collapsed=true;
Using the optioninfoText
, will create a info button, displaying the text you set, on hovering.
{"type":"string","title":"Name","options": {"infoText":"Your full name" }}
Using the optionenum
, it is possible to modify how enums with formatcheckbox
(default) are displayed in the editor.It is an array of objects (described below), which must be in the same order as defined with theenum
keyword.
Currently, the following is supported:
title
:Optional Display value shown instead of the enum valueinfoText
:Optional Creates an info button next to the title, displaying the text you set, on hovering.
It is possible also to set these options only for some enum values, to skip one enum value, define an empty object ({}
).
{"type":"array","items": {"type":"string","enum": ["1","2","3","4"],"options": {"enum": [ {}, {"title":"Title 2" }, {"infoText":"InfoText 3" }, {"title":"Title 4","infoText":"InfoText 4" } ] } }}
If both optionsenum_titles[x]
andenum[x].title
are set for the enum valuex
, than the title set underenum[x].title
will be used.
Sometimes, it's necessary to have one field's value depend on another's.
The dependency information is fetched from the dependencies field in the options field of the control. Thedependencies
field should be a map where the keys are the names of the fields depended on and the value is the expected value. The value may be an array to indicate multiple value possibilities. This uses the internal field value watch system to notify fields of changes in their dependencies.
Here's an example schema:
{"title":"An object","type":"object","properties": {"fieldOne": {"title":"I should be changed to 'foo'","type":"string","enum": ["foo","bar"],"default":"bar" },"depender1": {"title":"I depend on fieldOne to be 'foo'","type":"string","enum": ["lorem","ipsum"],"options": {"dependencies": {"fieldOne":"foo" } } },"depender2": {"title":"I depend on fieldOne to be 'bar'","type":"string","enum": ["dolor","sit"],"options": {"dependencies": {"fieldOne":"bar" } } } }}
Keys can also be an absolute path likeroot.property.nested_property
{"title":"Person","type":"object","required": ["gender" ],"properties": {"gender": {"title":"Gender","type":"string","enum": ["female","male" ] },"age": {"type":"object","properties": {"maleSpecificAge": {"type":"string","title":"Male specific age question?","options": {"dependencies": {"root.gender":"male" } } },"femaleSpecificAge": {"type":"string","title":"Female specific age question?","options": {"dependencies": {"root.gender":"female" } } } } } } }
Thedependencies
keyword from the JSON Schema specification is not nearly flexible enough to handle most use cases,so JSON Editor introduces a couple custom keywords that help in this regard.
The first step is to have a field "watch" other fields for changes.
{"type":"object","properties": {"first_name": {"type":"string" },"last_name": {"type":"string" },"full_name": {"type":"string","watch": {"fname":"first_name","lname":"last_name" } } }}
The keywordwatch
tells JSON Editor which fields to watch for changes.
The keys (fname
andlname
in this example) are alphanumeric aliases for the fields.
The values (first_name
andlast_name
) are paths to the fields. To access nested properties of objects, use a dot for separation (e.g. "path.to.field").
By default paths are from the root of the schema, but you can make the paths relative to any ancestor node with a schemaid
defined as well. This is especially useful within arrays. Here's an example:
{"type":"array","items": {"type":"object","id":"arr_item","properties": {"first_name": {"type":"string" },"last_name": {"type":"string" },"full_name": {"type":"string","watch": {"fname":"arr_item.first_name","lname":"arr_item.last_name" } } } }}
Now, thefull_name
field in each array element will watch thefirst_name
andlast_name
fields within the same array element.
Watching fields by itself doesn't do anything. For the example above, you need to tell JSON Editor thatfull_name
should befname [space] lname
.JSON Editor uses a javascript template engine to accomplish this. A barebones template engine is included by default (simple{{variable}}
replacement only), but many of the most popular template engines are also supported:
- ejs
- handlebars
- hogan
- markup
- mustache
- swig
- underscore >=1.7 (since 1.4.0, see also#332)
You can change the default by settingJSONEditor.defaults.options.template
to one of the supported template engines:
JSONEditor.defaults.options.template='handlebars';
You can set the template engine on a per-instance basis as well:
consteditor=newJSONEditor(element,{schema:schema,template:'hogan'});
Here is the completedfull_name
example using the default barebones template engine:
{ "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, "full_name": { "type": "string", "template": "{{fname}} {{lname}}", "watch": { "fname": "first_name", "lname": "last_name" } } }}
It is also possible to set the "template" property to a JavaScript callback function, defined underwindow.JSONEditor.defaults.callbacks.template
. Inside the JavaScript callback, you have access to all the variables defined under thewatch
property + the current editor.
Example Schema:
{ "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, "full_name": { "type": "string", "template": "callbackFunction", "watch": { "fname": "first_name", "lname": "last_name" } } }}
Example Callback function:
window.JSONEditor.defaults.callbacks.template = { "callbackFunction": (jseditor,e) => { return e.fname + " " + e.lname; }};
Another common dependency is a drop down menu whose possible values depend on other fields. Here's an example:
{"type":"object","properties": {"possible_colors": {"type":"array","items": {"type":"string" } },"primary_color": {"type":"string" } }}
Let's say you want to forceprimary_color
to be one of colors in thepossible_colors
array. First, we must tell theprimary_color
field to watch thepossible_colors
array.
{"primary_color": {"type":"string","watch": {"colors":"possible_colors" } }}
Then, we use the special keywordenumSource
to tell JSON Editor that we want to use this field to populate a drop down.
{"primary_color": {"type":"string","watch": {"colors":"possible_colors" },"enumSource":"colors" }}
Now, anytime thepossible_colors
array changes, the dropdown's values will be changed as well.
This is the most basic usage ofenumSource
. The more verbose form of this property supportsfiltering, pulling from multiple sources, constant values, etc..Here's a more complex example (this uses the Swig template engine syntax to show some advanced features)
{ // An array of sources "enumSource": [ // Constant values ["none"], { // A watched field source "source": "colors", // Use a subset of the array "slice": [2,5], // Filter items with a template (if this renders to an empty string, it won't be included) "filter": "{% if item !== 'black' %}1{% endif %}", // Specify the display text for the enum option "title": "{{item|upper}}", // Specify the value property for the enum option "value": "{{item|trim}}" }, // Another constant value at the end of the list ["transparent"] ]}
You can also specify a list of static items with a slightly different syntax:
{ "enumSource": [{ // A watched field source "source": [ { "value": 1, "title": "One" }, { "value": 2, "title": "Two" } ], "title": "{{item.title}}", "value": "{{item.value}}" }] ]}
The colors examples used an array of strings directly. Using the verbose form, you canalso make it work with an array of objects. Here's an example:
{ "type": "object", "properties": { "possible_colors": { "type": "array", "items": { "type": "object", "properties": { "text": { "type": "string" } } } }, "primary_color": { "type": "string", "watch": { "colors": "possible_colors" }, "enumSource": [{ "source": "colors", "value": "{{item.text}}" }] } }}
All of the optional templates in the verbose form have the propertiesitem
andi
passed into them.item
refers to the array element.i
is the zero-based index.
It is also possible to use JavaScript callback functions instead of templates for the enumSource properties properties:value
,title
andfilter
.
Example Schema:
{"type":"object","properties": {"possible_colors": {"type":"array","format":"table","items": {"type":"object","properties": {"text": {"type":"string" } } } },"primary_color": {"type":"string","watch": {"colors":"possible_colors" },"enumSource": [{"source":"colors","filter":"enumFilterCB","title":"enumTitleCB","value":"enumValueCB" }] } }}
Example JavaScript callbacks:
window.JSONEditor.defaults.callbacks.template={"enumFilterCB":(jseditor,e)=>{if(e.item.text.toLowerCase()=='red')return"";// "red" is not allowedreturne.item.text;},"enumTitleCB":(jseditor,e)=>e.item.text.toUpperCase(),"enumValueCB":(jseditor,e)=>e.item.text.toLowerCase()};
To sort the dynamic EnumSource, you can set the EnumSource propertysort
to eitherasc
ordesc
.
Thetitle
keyword of a schema is used to add user friendly headers to the editing UI. Sometimes though, dynamic headers, which change based on other fields, are helpful.
Consider the example of an array of children. Without dynamic headers, the UI for the array elements would showChild 1
,Child 2
, etc..It would be much nicer if the headers could be dynamic and incorporate information about the children, such as1 - John (age 9)
,2 - Sarah (age 11)
.
To accomplish this, use theheaderTemplate
property. All of the watched variables are passed into this template, along with the static titletitle
(e.g. "Child"), the 0-based indexi0
(e.g. "0" and "1"), the 1-based indexi1
, extra child variableproperties.${PROPERTY_NAME}.enumTitle
and the field's valueself
(e.g.{"name": "John", "age": 9}
).
{ "type": "array", "title": "Children", "items": { "type": "object", "title": "Child", "headerTemplate": "{{ i1 }} - {{ self.name }} (age {{ self.age }}) has a {{ properties.pet.enumTitle }}", "properties": { "name": { "type": "string" }, "age": { "type": "integer" }, "pet": { "title": "Pet", "type": "string", "enum": [ "pet_1", "pet_2" ], "options": { "enum_titles": [ "Dog", "Cat" ] } } } }}
If one of the included template engines isn't sufficient,you can use any custom template engine with acompile
method. For example:
constmyengine={compile:template=>// Compile should return a render functionvars=>{// A real template engine would render the template hereconstresult=template;returnresult;}};// Set globallyJSONEditor.defaults.options.template=myengine;// Set on a per-instance basisconsteditor=newJSONEditor(element,{schema:schema,template:myengine});
JSON Editor uses a translate function to generate strings in the UI. A defaulten
language mapping is provided.
You can easily override individual translations in the default language or create your own language mapping entirely.
// Override a specific translationJSONEditor.defaults.languages.en.error_minLength = "This better be at least {{0}} characters long or else!";// Create your own language mapping// Any keys not defined here will fall back to the "en" languageJSONEditor.defaults.languages.es = { error_notset: "propiedad debe existir"};
By default, all instances of JSON Editor will use theen
language. To override this default, set theJSONEditor.defaults.language
property.
JSONEditor.defaults.language="es";
You can also override translations per editor in the it's schema options.
"error_const": {"type":"string","title":"error_const","const":"test","default":"something else","options": {"error_messages": {"en": {"error_const":"CUSTOM EN: Value must be the constant value" },"es": {"error_const":"CUSTOM ES: Value must be the constant value" } } }}
All buttons have classnames in the formatjson-editor-btntype-*
. Using these classnames you can choose if the button should have icon or label hidden. The icon is wrapped in anI
tag, the label is wrapped in aSPAN
tag.
Examples:
Hide the icon on "Object Properties" button:
.json-editor-btntype-propertiesi {display: none;}
Hide the text on "Object Properties" button:
.json-editor-btntype-propertiesspan {display: none;}
JSON Editor contains editor interfaces for each of the primitive JSON types as well as a few other specialized ones.
You can add custom editors interfaces fairly easily. Look at any of the existing ones for an example.
JSON Editor uses resolver functions to determine which editor interface to use for a particular schema or subschema.
Let's say you make a customlocation
editor for editing geo data. You can add a resolver function to use this custom editor when appropriate. For example:
// Add a resolver function to the beginning of the resolver list// This will make it run before any other onesJSONEditor.defaults.resolvers.unshift(schema=>{if(schema.type==="object"&&schema.format==="location"){return"location";}// If no valid editor is returned, the next resolver function will be used});
The following schema will now use this custom editor for each of the array elements instead of the defaultobject
editor.
{"type":"array","items": {"type":"object","format":"location","properties": {"longitude": {"type":"number" },"latitude": {"type":"number" } } }}
If you create a custom editor interface that you think could be helpful to others, submit a pull request!
The possibilities are endless. Some ideas:
- A compact way to edit objects
- Radio button version of the
select
editor - Autosuggest for strings (like enum, but not restricted to those values)
- Better editor for arrays of strings (tag editor)
- Canvas based image editor that produces Base64 data URLs
JSON Editor provides a hook into the validation engine for adding your own custom validation.
Let's say you want to force all schemas withformat
set todate
to match the patternYYYY-MM-DD
.
// Custom validators must return an array of errors or an empty array if validJSONEditor.defaults.custom_validators.push((schema,value,path)=>{consterrors=[];if(schema.format==="date"){if(!/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(value)){// Errors must be an object with `path`, `property`, and `message`errors.push({path:path,property:'format',message:'Dates must be in the format "YYYY-MM-DD"'});}}returnerrors;});
// override class methodJSONEditor.defaults.editors.integer.prototype.sanitize=function(value){returnvalue};
or
// override object methodvarpath="root.integerfield";editor.getEditor(path).sanitize=function(value){returnvalue};
JSON Editor can be used with browsers that support ES5, but in order to use it with older browsers such as IE, it is necessary to use "core-js" as a polyfill.
<script src="https://unpkg.com/core-js-bundle@latest/minified.js"></script><script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
About
JSON Schema Based Editor
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.