v1.2.2

API Reference: JSONEditorNode

Last updated


This page lists the public fields and methods of JSONEditorNode. Nodes are created internally by JSONEditor; the class itself is not exported, so access nodes through editor.children and each node's children.

Properties


PropertyTypeDefaultDescription
idstringRandom 64-character stringUsed in DOM ids such as key-{id} and value-{id}
keystring""Node key name; empty string for array children. Newlines and surrounding whitespace are stripped while editing
typestring"string""string" / "number" / "boolean" / "object" / "array" — see Type System
valuestring""Raw node value as a string (non-container types only)
parentJSONEditor | JSONEditorNode—Reference to the parent node
childrenJSONEditorNode[][]Child nodes (used by object / array types)
collapsedbooleanfalseCurrent collapsed state; nodes created by JSONEditor start from config.collapsed (default true)

Methods


MethodDescription
render()Renders and returns this node's DOM element (calls the private #create())
addChild()Appends an empty child, inserts it into the DOM, and triggers Lifecycle.update; the new node does not inherit collapsed / confirmKeyRemove — see Core Concepts
updateChild()Rebuilds this node's DOM and triggers Lifecycle.update
setCollapsed()Toggles the collapsed state and rebuilds this node's DOM without triggering Lifecycle.update
json (getter)Returns this node's JavaScript value (recursive) — see Serialization

Example


Walking editor.children recursively and printing each node's key / type / value, then the first node's json:
  1. <section id="editor"></section>
  2. <script>
  3. const editor = new JSONEditor({
  4. id: "editor",
  5. fill: false,
  6. json: { user: { name: "Pardn", langs: ["Go", "JS"] } },
  7. when: {
  8. rendered: () => {
  9. const walk = (nodes, depth) => nodes.forEach((n) => {
  10. console.log(" ".repeat(depth) + (n.key || "-"), n.type, n.value);
  11. walk(n.children, depth + 1);
  12. });
  13. walk(editor.children, 0);
  14. console.log(editor.children[0].json);
  15. },
  16. },
  17. });
  18. </script>
Live preview · @pardnchiu/nanojson@1.2.2

Further Reading


中文