nanocoai/nanoclaw · error · StdinJsonInputError

--stdin-json input is not valid JSON

Error message

--stdin-json input is not valid JSON

What it means

JSON.parse of the --stdin-json input failed; the underlying SyntaxError is attached as {cause}. The input must be a single syntactically valid JSON document.

Source

Thrown at src/cli/stdin-json.ts:72

      throw new StdinJsonInputError(`--stdin-json input exceeds ${MAX_STDIN_JSON_BYTES} bytes`);
    }
    chunks.push(buffer);
  }

  return Buffer.concat(chunks, byteLength).toString('utf8');
}

/** Parse the input, requiring exactly one JSON object — not an array, scalar, or null. */
function parseJsonObject(source: string): Record<string, unknown> {
  if (source.trim().length === 0) {
    throw new StdinJsonInputError('--stdin-json input is empty');
  }

  let parsed: unknown;
  try {
    parsed = JSON.parse(source);
  } catch (err) {
    throw new StdinJsonInputError('--stdin-json input is not valid JSON', { cause: err });
  }

  if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
    throw new StdinJsonInputError('--stdin-json input must be one JSON object');
  }

  return parsed as Record<string, unknown>;
}

/** Match the key normalization applied by command parsers in crud.ts. */
function canonicalArgKey(key: string): string {
  return key.replace(/-/g, '_');
}

/**
 * Reject any stdin key that could collide with another arg after the merge.
 *
 * Command parsers (crud.ts) normalize `-` to `_` in arg keys, so `group-id`

View on GitHub (pinned to 294ef2aee8)

Solutions

  1. Validate with a JSON linter or `jq . < file` first
  2. Regenerate the file programmatically instead of hand-editing
  3. Fix the reported syntax error position from the cause

Example fix

# before
echo '{"name": "x",}' | ncl groups create --stdin-json
# after
echo '{"name": "x"}' | ncl groups create --stdin-json
Defensive patterns

Strategy: try-catch

Validate before calling

try { JSON.parse(src); } catch { throw new Error('invalid JSON'); }

Type guard

function parsesAsJson(s: string): boolean { try { JSON.parse(s); return true; } catch { return false; } }

Try / catch

try { JSON.parse(source); } catch (err) { throw new StdinJsonInputError('--stdin-json input is not valid JSON', { cause: err }); }

Prevention

When it happens

Trigger: Piping malformed JSON — trailing commas, comments, smart quotes, truncated files, or concatenated objects.

Common situations: Hand-written JSON in heredocs, JSON produced by echoing shell variables unquoted, or a truncated file.

Related errors


AI-assisted analysis of nanocoai/nanoclaw@294ef2aee8 (2026-08-28). Data as JSON: /api/errors/c917671e2d1eb206. Report an issue: GitHub.