Appifio Creator · Lesson EM02 · Easy Storage

JSON & first-run empty
the two Easy rules that break most often

Don’t apply one parse rule to every read. Easy ≠ Aura for parsing and for readField.

Learning goals: Know when you must JSON.parse; handle “notebook not created yet” without panicking.

Previous: EM01 · Next: EM03

1. Which methods need parse?

MethodWhat you getWhat you do
readFilecontent = raw stringJSON.parse in try/catch (if it’s JSON)
readFsFile (html/json/css…)Raw stringParse if JSON; use HTML as a string
readFieldvalue is already a JS valueDo not parse again
getRoutesRoutes objectUse as-is
list / exists / upload…Metadata / urlDon’t apply the readFile rule
Aura: readFile already returns an object. Easy: still a string → forget to parse and the app looks “empty” or crashes. Don’t mix the two models.

2. Correct example - readFile

await window.waitForAppStorage?.();

const result = await appifio_client.appifio_readFile('blog.json');
if (result.success && result.data.exists && result.data.content) {
  try {
    const data = JSON.parse(result.data.content);
    // use data.blogs, data.metadata…
  } catch (e) {
    console.warn('Invalid JSON', e);
  }
}

3. Correct example - readField (no parse)

const field = await appifio_client.appifio_readField('settings.json', 'theme.color');
if (field.success) {
  const color = field.data.value; // already ready - do NOT JSON.parse(color)
} else {
  // Easy: file or field missing → success: false (unlike Aura value: null)
}

Field details: EM04 / EM03.

4. “File not found yet” is normal

On first run the notebook may not exist. Easy behaves differently by method:

MethodWhen the file does not exist
readFilesuccess: true, content: "", exists: false - not a 404
fileExistssuccess: true, exists: false
readFieldsuccess: false (not Aura’s value: null)
readFsFile (page file)success: false if the path is missing
getFileInfosuccess: false

5. Golden pattern: always start with a default structure

async function loadBlogs() {
  // 1) Default BEFORE reading
  let blogIndex = {
    blogs: {},
    metadata: { total: 0, last_updated: '' }
  };

  await window.waitForAppStorage?.();
  const result = await appifio_client.appifio_readFile('blog.json');

  // 2) Replace only when read OK and the file really exists
  if (result?.success && result.data?.exists && result.data.content) {
    try {
      blogIndex = JSON.parse(result.data.content);
    } catch (e) {
      console.warn(e);
    }
  }
  return blogIndex;
}
Wrong: assume the file always exists → JSON.parse(undefined) or crash. Right: default first, then merge real data.

Next

EM03 - Read / write one JSON notebook (single index)

Appifio Creator · User guide · EM02