BigPizzaV3/CodexPlusPlus · error
must be a JSON object
Error message
{} must be a JSON object What it means
`load_global_state` reads the codex global state JSON file, parses it with serde_json, and requires the top-level value to be a JSON object (`Map<String, Value>`). If the file parses as valid JSON but is an array, string, number, or boolean, this error is raised with the file path. It guards downstream code that indexes the state by key.
Solutions
- Inspect the file named in the message and restore it to a JSON object (e.g. `{}`)
- Delete/regenerate the state file so codex rewrites a fresh object
- Check for concurrent writers (two processes) clobbering the file and serialize access
- Treat non-object content as empty state instead of erroring, if recovery is preferred
Example fix
// before
value.as_object().cloned().map(Some)
.ok_or_else(|| anyhow::anyhow!("{} must be a JSON object", path.display()))
// after
Ok(value.as_object().cloned().or_else(|| {
tracing::warn!("{} was not a JSON object; resetting to empty state", path.display());
Some(Map::new())
})) Defensive patterns
Strategy: validation
Validate before calling
let text = std::fs::read_to_string(&path)?;
let v: serde_json::Value = serde_json::from_str(&text)?;
if !v.is_object() {
eprintln!("{} is not a JSON object; resetting", path.display());
} Type guard
// Rust narrowing
fn as_state_object(v: &serde_json::Value) -> Option<&serde_json::Map<String, serde_json::Value>> {
v.as_object()
} Try / catch
match load_global_state(home) {
Err(e) if e.to_string().contains("must be a JSON object") => {
// quarantine bad file and continue with empty state
std::fs::rename(&state_path, state_path.with_extension("bad"))?;
Map::new()
}
Err(e) => return Err(e),
Ok(state) => state.unwrap_or_default(),
} Prevention
- Never hand-edit the codex state file; write whole objects atomically
- Use temp-file + rename for state writes to avoid partial/scalar content
- Validate the JSON top-level type after any external tool touches the file
When it happens
Trigger: Calling `load_global_state` (via `capture_app_state_snapshot` or `sync_app_state_after_provider_switch`) when the state file contains e.g. `[]`, `"text"`, `123`, or `null` instead of `{...}`.
Common situations: The file was hand-edited or overwritten by another tool writing a non-object JSON value, a truncation/partial write left a scalar, or a version change changed the state file schema.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19).
Data as JSON: /api/errors/2299042bacaffefe.
Report an issue: GitHub.
Appendix: source
Thrown at crates/codex-plus-core/src/codex_app_state.rs:182
);
}
}
}
fn load_global_state(home: &Path) -> anyhow::Result<Option<Map<String, Value>>> {
let path = state_path(home);
if !path.exists() {
return Ok(None);
}
let text =
fs::read_to_string(&path).with_context(|| format!("failed to read {}", path.display()))?;
let value: Value = serde_json::from_str(&text)
.with_context(|| format!("failed to parse {}", path.display()))?;
value
.as_object()
.cloned()
.map(Some)
.ok_or_else(|| anyhow::anyhow!("{} must be a JSON object", path.display()))
}
fn load_snapshot(home: &Path) -> anyhow::Result<Option<Map<String, Value>>> {
let path = snapshot_path(home);
if !path.exists() {
return Ok(None);
}
let text =
fs::read_to_string(&path).with_context(|| format!("failed to read {}", path.display()))?;
let value: Value = serde_json::from_str(&text)
.with_context(|| format!("failed to parse {}", path.display()))?;
let state = value
.get("state")
.and_then(Value::as_object)
.or_else(|| value.as_object())
.cloned()
.unwrap_or_default();
Ok(Some(state))View on GitHub (pinned to b1ed92e5e4)