Overview
Reference usage
Controlled free-form JSON editing on a lazily loaded CodeMirror 6 editor. It adds line numbers, code folding, bracket matching, schema completion, inline syntax errors, a Format action, and a height that fits the content, and it keeps a native textarea behind it for form posting and as a fallback.
The editor runtime is a separate chunk. It loads through a dynamic import() when the first JsonEditor mounts, so a page without one never downloads it. Until it arrives, on the server, and if it fails to load, the textarea is the editor. The value, onValueChange, id, label, and form posting contract is the same in both modes, and the textarea carries any name or form attribute you pass, so the field posts whichever editor is showing.
Usage
Empty values
An empty optional field is valid. It shows no error and no lint glyph, announces no "Valid JSON.", and reads "Not set" through its placeholder. Pass required to make an empty value an error again, or pass placeholder to replace the text.
Read-only and disabled
readonly keeps the field focusable and selectable and posts it with its form, but refuses edits and disables Format, in the editor and in the textarea fallback. disabled also removes it from the form post and from the tab order.
Required
required keeps the native constraint on the posted textarea after the editor mounts, so an empty required field blocks form submission. The textarea is hidden and cannot take focus, so the invalid-field focus is sent to the visible editor.
Height
The editor grows with its content, so a short value is not a large empty box. rows is the most lines it shows before it scrolls, and it defaults to 8.
Schema completion
Pass a JSON Schema as schema to complete property names and values. Completion follows properties, items, local $ref pointers, and allOf, anyOf, and oneOf, and it offers enum and const values and true or false for booleans. It never rejects what the user types, and a keyword it does not read is ignored.
Then pass it to the component with schema={schema}.
Format
The Format button and Shift+Alt+F rewrite the document as JSON indented by two spaces. Both are disabled for empty or invalid JSON. Formatting parses and re-serializes the document, so digits beyond double precision are lost and duplicate keys collapse.
Enter the JSON body sent with the request.
Valid JSON.
When to use
- Editing arbitrary JSON source while preserving the exact string in parent-owned state.
- The value is genuinely free-form JSON and benefits from line numbers, folding, and completion.
- The value has a known shape that a guided form can edit. Use Schema Form instead
- Editing a JSON Schema with guided form and diff views. Use JSON Schema Editor instead
- Displaying JSON without allowing changes. Use JSON Viewer instead
Examples
Controlled JSON editor
A controlled JSON editor with line numbers, folding, inline errors, and a Format action.
Props
Name | Type | Default | Description |
|---|---|---|---|
id required | text | Unique identifier used for the label and feedback relationships. | |
value required | text | Controlled JSON source text. | |
label required | text | Visible label for the editor. | |
description | text | Supporting text announced with the editor. | |
error | text | External validation error. Takes precedence over JSON parse feedback. | |
rows | number | 8 | The most lines the editor shows before it scrolls. It grows with its content up to this height, so a short value is not a large empty box. Defaults to 8. |
validFeedbackVisible | boolean | true | Whether valid JSON should render an announced success message. Defaults to true. |
schema | JsonEditorSchema | A JSON Schema that drives property-name and value completion. Without one the editor offers no completions. | |
required | boolean | false | Marks the editor as required. An empty value is valid and reads "Not set" unless the field is required. |
placeholder | text | ||
disabled | boolean | false | Disables the editor. A disabled field is not posted with its form. |
readonly | boolean | false | Makes the editor read-only. A read-only field stays focusable and selectable and is posted with its form, but cannot be edited or formatted. |
onValueChange | (value: string) => void | Called with the proposed JSON source whenever the user edits the field. | |
autofocus | boolean | false | |
wrap | 'hard''soft''off'undefinednull |