Skip to Content

React JSON Editor

Edit, format, and validate serialized JSON in a labeled React textarea with controlled source text and typed parse results.

Use JsonEditor to edit serialized JSON text. It provides explicit format and validation actions while leaving schema rules, persistence, and submission to the caller. For authored rich text, use TextEditor instead; JSON is not a rich-text document format.

Beta API maturity: Beta. The API is available for evaluation and may change before it is considered stable.

Import

import { JsonEditor } from "@puzzle-ui/ui";
import type { JsonEditorResult, JsonValue } from "@puzzle-ui/ui";

Controlled editor

Pass value and onChange when the caller owns source text. validate receives parsed JSON and returns an error string or null. The onValidate callback receives a discriminated result: valid JSON includes a JsonValue; parse and validation errors include a typed type and message.

390 × 844

Use defaultValue for uncontrolled source text. Formatting parses the source, runs the optional validator, and applies the configured indentation. Validation errors do not prevent formatting syntactically valid JSON. The validation success message is shown only when parsing and the caller validator both pass. Numbers outside JavaScript's finite range are reported as parse errors and are not formatted, because serializing them would silently replace them with null.

API

PropTypeDescription
labelReactNodeVisible label for the textarea. Required.
valuestringControlled JSON source text.
defaultValuestringInitial source for uncontrolled usage.
onChange(source: string) => voidCalled when the source text changes.
descriptionReactNodeSupporting text associated with the textarea.
validate(value: JsonValue) => string | nullOptional synchronous schema validation.
onValidate(result: JsonEditorResult) => voidCalled after explicit validation or formatting.
formatIndentnumberIndentation width used by Format; defaults to 2.
formatLabel, validateLabelstringVisible and accessible action labels.
successMessagestringStatus text shown after successful validation.
errorReactNode | booleanExternal validation message; true uses a default message.
invalidbooleanExplicit invalid state.
rowsnumberInitial visible textarea rows; defaults to 12.
required, disabled, readOnlybooleanNative textarea states.
rootClassName, classNamestringAdditional root and textarea classes.
Native textarea attributes-Forwarded to the textarea, including id, name, and aria-*.

JsonValue is the recursive JSON value type. JsonEditorResult is a discriminated union of { valid: true; value } and { valid: false; error }; JsonEditorError.type is "parse" or "validation".

Accessibility and compatibility

The visible label is associated with a native textarea. The description and errors are connected with aria-describedby; invalid values set aria-invalid, and validation feedback is announced. Format and Validate are native buttons with visible focus styles. The editor uses no browser API during render and uses Puzzle theme tokens.

UI targets React 19; verify framework-specific SSR and hydration behavior in your application.