{"record":{"id":"7ac321dbc5282f9f","repo":"Yeachan-Heo/oh-my-codex","slug":"answer-must-be-valid-json-error-as-error-me","errorCode":null,"errorMessage":"--answer must be valid JSON: ${(error as Error).message}","messagePattern":"--answer must be valid JSON: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/cli/question.ts","lineNumber":259,"sourceCode":"  const parsed = parseQuestionArgs(args);\n  if (parsed.help || args.length === 0) {\n    console.log(QUESTION_HELP);\n    return;\n  }\n\n  if (parsed.ui) {\n    if (!parsed.statePath) throw new Error('--ui requires --state-path');\n    await runQuestionUi(parsed.statePath);\n    return;\n  }\n\n  if (parsed.answerQuestionId) {\n    if (!parsed.answer) throw new Error('--answer-question-id requires --answer');\n    let answerPayload: unknown;\n    try {\n      answerPayload = JSON.parse(parsed.answer);\n    } catch (error) {\n      throw new Error(`--answer must be valid JSON: ${(error as Error).message}`);\n    }\n    try {\n      const { record, recordPath } = await submitQuestionAnswerById(\n        process.cwd(),\n        parsed.answerQuestionId,\n        answerPayload,\n        { sessionId: parsed.sessionId },\n      );\n      printJson({\n        ok: true,\n        question_id: record.question_id,\n        session_id: record.session_id,\n        status: record.status,\n        answers: record.answers ?? [],\n        record_path: recordPath,\n      }, parsed.json);\n    } catch (error) {\n      const code = error instanceof QuestionSubmitError ? error.code : 'question_submit_failed';","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/Yeachan-Heo/oh-my-codex/blob/3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2/src/cli/question.ts#L241-L277","documentation":"Thrown when the value passed to --answer cannot be parsed as JSON; the underlying JSON.parse SyntaxError message is appended. The answer payload must be a valid JSON document because it is deserialized and forwarded to submitQuestionAnswerById.","triggerScenarios":"`omx question --answer-question-id <id> --answer <value>` where <value> is not valid JSON — e.g. bare strings like yes, trailing commas, or smart quotes from shell interpolation.","commonSituations":"Passing plain text instead of a quoted JSON string, single-vs-double quote mistakes in shells, trailing commas, or newlines inserted by variable expansion.","solutions":["Wrap the answer in double quotes and make it valid JSON: --answer '\"yes\"' for a string, --answer '{\"a\":1}' for an object","Check for trailing commas and unescaped inner quotes","Echo the exact value through `jq .` to confirm it parses before retrying"],"exampleFix":"# before\nomx question --answer-question-id q_1 --answer yes\n# after\nomx question --answer-question-id q_1 --answer '\"yes\"'","handlingStrategy":"validation","validationCode":"function assertValidJson(value: string, label: string): unknown {\n  try { return JSON.parse(value); }\n  catch (e) { throw new Error(`${label} is not valid JSON: ${(e as Error).message}`); }\n}\nconst payload = assertValidJson(answerJson, '--answer');","typeGuard":null,"tryCatchPattern":"try {\n  await questionCommand([...argv]);\n} catch (error) {\n  if (error instanceof Error && error.message.startsWith('--answer must be valid JSON')) {\n    // re-serialize the payload with JSON.stringify and retry\n  } else throw error;\n}","preventionTips":["Always build the --answer value with JSON.stringify, never string concatenation","Validate payloads with JSON.parse before passing them to the CLI","Prefer single-quoted shell strings around JSON"],"tags":["cli","json-parse","question","validation"],"backgroundTag":"invalid-json-input","analyzedSha":"3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2","analyzedAt":"2026-08-27T22:18:39.783Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}