v1.2.1

Getting Started

Last updated


This page covers installing NanoJSON and mounting your first editor instance.

Prerequisites


  • A browser with ES2022 class private fields and methods (#field, #method()), such as Chrome 90+, Firefox 90+, Safari 15+, Edge 90+
  • Browser only: the library uses window, document, FileReader, fetch, and confirm(), and has no runtime dependency
  • Node.js 18+ (only for building from source)

Installation


npm


  1. npm install @pardnchiu/nanojson

CDN (UMD)


  1. <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/nanojson/dist/NanoJSON.js"></script>

ES Module


  1. import { JSONEditor } from "https://cdn.jsdelivr.net/npm/@pardnchiu/nanojson/dist/NanoJSON.esm.js";

CSS is injected automatically when JSONEditor is constructed (loadCSS, in src/data.js); to use a custom stylesheet, pass a css path in the config to override the default CDN URL.

First Mount


Prepare a mount point in HTML and initialize the editor with an id:
  1. <div id="editor"></div>
  2. <script>
  3. const editor = new JSONEditor({ id: "editor" });
  4. </script>

If id is omitted, JSONEditor creates a new <section class="pd-json-editor"> element (available as editor.body) but does not attach it to the page — you need to appendChild it yourself. editor.body is assigned asynchronously after data loading, so it is still undefined right after the constructor returns; attach it inside when.beforeRender or when.rendered:
  1. const editor = new JSONEditor({
  2. json: { name: "NanoJSON" },
  3. when: {
  4. rendered: () => document.querySelector("#host").appendChild(editor.body),
  5. },
  6. });

Example


Mounting into #editor with initial data:
  1. <section id="editor"></section>
  2. <script>
  3. const editor = new JSONEditor({
  4. id: "editor",
  5. fill: false,
  6. json: { name: "NanoJSON", version: "1.2.1" },
  7. });
  8. </script>
Live preview · @pardnchiu/nanojson@1.2.2

Next Steps


中文