Yeachan-Heo/oh-my-codex · error

${source} must decode to a JSON object

Error message

${source} must decode to a JSON object

What it means

The JSON input parsed successfully but decoded to null, a primitive (string/number/boolean), or an array. The state command requires a top-level JSON object so it can spread keys into state.

Source

Thrown at src/cli/state.ts:62

}

function parseStateInputJson(
  raw: string,
  source: '--input' | '--input-file',
): Record<string, unknown> {
  const json = source === '--input-file' && raw.startsWith('\uFEFF') ? raw.slice(1) : raw;
  let parsed: unknown;
  try {
    parsed = JSON.parse(json);
  } catch (error) {
    let message = `${source} must be valid JSON: ${(error as Error).message}`;
    if (source === '--input' && looksLikeQuoteStrippedJson(raw)) {
      message += WINDOWS_QUOTE_HINT;
    }
    throw new Error(message);
  }
  if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
    throw new Error(`${source} must decode to a JSON object`);
  }
  return { ...(parsed as Record<string, unknown>) };
}

export async function stateCommand(
  args: string[],
  deps: StateCommandDependencies = {},
): Promise<void> {
  const stdout = deps.stdout ?? ((line: string) => console.log(line));
  const stderr = deps.stderr ?? ((line: string) => console.error(line));
  const execute = deps.execute ?? executeStateOperation;

  const subcommand = args[0];
  if (!subcommand || isHelpArg(subcommand)) {
    stdout(STATE_HELP);
    return;
  }

View on GitHub (pinned to 3ad79a8a6f)

Solutions

  1. Wrap the payload as an object: `{"data": <your array>}` if an array is what you have
  2. Use `jq '{key: .}'` to construct an object

Example fix

# before
state set --input "$(jq '.items' data.json)"
# after
state set --input "$(jq '{items: .items}' data.json)"
Defensive patterns

Strategy: type-guard

Validate before calling

const parsed = JSON.parse(text);
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
  throw new TypeError('state input must be a JSON object');
}

Type guard

function isJsonObject(v: unknown): v is Record<string, unknown> {
  return typeof v === 'object' && v !== null && !Array.isArray(v);
}

Prevention

When it happens

Trigger: `state set --input '[1,2,3]'`, `--input '"hello"'`, or `--input 'null'`.

Common situations: Passing a JSON array from jq output (`jq '.items'`), or a scalar where an object was expected; API payloads nested one level too deep.

Related errors


AI-assisted analysis of Yeachan-Heo/oh-my-codex@3ad79a8a6f (2026-08-27). Data as JSON: /api/errors/dedd41e2e19a3c8d. Report an issue: GitHub.