jlcodes99/cockpit-tools · error

invalidJsonMessage

Error message

invalidJsonMessage

What it means

parseLineDelimitedJsonObjects parses multi-line input where each line must be a standalone JSON object. This error is thrown when a line cannot be parsed by JSON.parse (or is not a plain object). The hook falls back to line-delimited parsing only when the whole content is not valid JSON, so hitting this means neither whole-bundle nor line-delimited JSON parsing succeeded.

Source

Thrown at src/hooks/useProviderAccountsPage.ts:277

};

const parseLineDelimitedJsonObjects = (
  rawContent: string,
  invalidJsonMessage: string,
): unknown[] | null => {
  const lines = rawContent
    .split(/\r?\n/)
    .map((line) => line.trim())
    .filter((line) => line.length > 0);

  if (lines.length <= 1) return null;

  return lines.map((line) => {
    let parsed: unknown;
    try {
      parsed = JSON.parse(line) as unknown;
    } catch {
      throw new Error(invalidJsonMessage);
    }
    if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
      throw new Error(invalidJsonMessage);
    }
    return parsed;
  });
};

const isCodexDirectImportItem = (value: unknown): boolean => {
  if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
  const payload = value as Record<string, unknown>;
  const tokens = payload.tokens;
  if (
    typeof payload.id_token === 'string' &&
    payload.id_token.trim() &&
    typeof payload.access_token === 'string' &&
    payload.access_token.trim()
  ) {

View on GitHub (pinned to 1ed8b77992)

Solutions

  1. Check the exact line number in the pasted content and fix its JSON syntax (missing quotes, commas, braces)
  2. Validate each line individually with JSON.parse before pasting
  3. If the content is not JSON (e.g. raw tokens or prose), use the plain refresh-token format instead (codex) or export proper JSON
  4. Ensure each line is a single self-contained JSON object, not an array or comma-joined objects

Example fix

// before (line 2 invalid)
{"refresh_token":"rt_a"}
{refresh_token: "rt_b"}
// after
{"refresh_token":"rt_a"}
{"refresh_token":"rt_b"}
Defensive patterns

Strategy: validation

Validate before calling

const lines = content.split(/\r?\n/).map(l => l.trim()).filter(Boolean);
if (lines.length > 1 && !lines.every(l => { try { const v = JSON.parse(l); return !!v && typeof v === 'object' && !Array.isArray(v); } catch { return false; } })) {
  throw new Error('Each line must be a valid JSON object');
}

Type guard

const isPlainObject = (v: unknown): v is Record<string, unknown> =>
  !!v && typeof v === 'object' && !Array.isArray(v);

Try / catch

try {
  items = resolveExternalImportBundleItems(content, platformId, messages);
} catch (e) {
  if (e instanceof Error && e.message === messages.invalidJson) {
    showLineByLineJsonHelp(content);
  }
}

Prevention

When it happens

Trigger: resolveExternalImportBundleItems fails JSON.parse on the whole content, then the line-delimited fallback parseLineDelimitedJsonObjects encounters a line with invalid JSON syntax (line 277: parse failure).

Common situations: Pasting export files where one line is truncated or hand-edited; lines containing unquoted or trailing-comma JSON; mixed content like log lines or comments interleaved with JSON objects; copy-paste losing characters.

Related errors


AI-assisted analysis of jlcodes99/cockpit-tools@1ed8b77992 (2026-09-05). Data as JSON: /api/errors/303796e95d1b4030. Report an issue: GitHub.