v1.2.2

Data Loading

Last updated


This page explains which data sources NanoJSON accepts, how the initial config and import() load data, and what actually happens when loading fails.

Initial Data Config


The json / file / path config fields specify the initial data. The first non-null value of config.file ?? config.json ?? config.path is used (JSONEditor.js:60), so file wins when several are given:
  1. // From a JavaScript object
  2. const editor = new JSONEditor({
  3. id: "editor",
  4. json: { name: "NanoJSON", version: "1.2.1" },
  5. });
  6. // From a remote URL
  7. const editor = new JSONEditor({
  8. id: "editor",
  9. file: "https://example.com/data.json",
  10. });

If none is provided, the editor starts with an empty object {} and calls insert() once to add an empty node.

Data Sources and Failure Behavior


Every source goes through getJSON() (src/function/getJSON.js):
InputHandlingOn Failure
null / undefinedTreated as no dataLoads {}
FileFileReader.readAsText, then JSON.parseParse errors log Failed to parse JSON from file ... and load {}
Object or arrayUsed as is—
string (URL)fetch, then JSON.parseNetwork or parse errors log Failed to fetch data from ... and load {}; a non-2xx HTTP status never resolves the Promise (getJSON.js:29), so initialization stalls at loading and import() never re-renders
Any other type (number, boolean)Logs Invalid data [_type]: ...Loads {}

Arrays load, but a root-level array is not written back out as an array — see Serialization.

Runtime Import: import() and reset()


editor.import(data) accepts the same sources, rebuilds every node through #jsonToChildren(), then calls render(true); once the initial render has completed, this triggers Lifecycle.update (beforeUpdate / updated after the 300ms debounce).
editor.reset() is equivalent to import({}). Unlike initialization, import() does not call insert() when the result is empty, so after a reset the editor has no nodes until you press "Add" or call insert().

Built-in Buttons


The footer buttons map to these actions:
ButtonShown WhenAction
AddAlwaysinsert()
Openbutton.import is truthyOpens a file picker with accept=".json", then import(file)
Downloadbutton.export is truthyexport() after confirm("Download?")
Resetbutton.reset is truthyimport({}) after confirm("Reset?")

Example


Loading from a URL string (fetch, then JSON.parse):
  1. <section id="editor"></section>
  2. <script>
  3. new JSONEditor({
  4. id: "editor",
  5. fill: false,
  6. file: "https://cdn.jsdelivr.net/npm/@pardnchiu/nanojson@1.2.1/package.json",
  7. });
  8. </script>
Live preview · @pardnchiu/nanojson@1.2.2

Further Reading


中文