{"record":{"id":"1e2a8e087cc26056","repo":"farion1231/cc-switch","slug":"configuration-must-be-a-json-object-not-an-array","errorCode":null,"errorMessage":"Configuration must be a JSON object, not an array or other type","messagePattern":"Configuration must be a JSON object, not an array or other type","errorType":"validation","errorClass":"PiFormValidationError","httpStatus":null,"severity":"error","filePath":"src/components/providers/forms/PiProviderForm.tsx","lineNumber":1071,"sourceCode":"      setProviderCompat(value);\n    },\n    [updateSettingsConfig],\n  );\n\n  const handleProviderKeyChange = useCallback((value: string) => {\n    const normalized = value.toLowerCase().replace(/[^a-z0-9-]/g, \"\");\n    setProviderKey(normalized);\n  }, []);\n\n  const submit = async (identity: ProviderFormData) => {\n    onSubmittingChange?.(true);\n    setFormError(null);\n    try {\n      if (!isEdit && selectedPresetId === null) {\n        throw new PiFormValidationError(t(\"pi.form.selectPresetRequired\"));\n      }\n      if (!parseJsonObject(identity.settingsConfig)) {\n        throw new PiFormValidationError(\n          t(\"jsonEditor.mustBeObject\"),\n          \"#pi-settings-config\",\n        );\n      }\n      const trimmedName = identity.name.trim();\n      const trimmedKey = providerKey.trim();\n      if (!trimmedName) {\n        throw new PiFormValidationError(\n          t(\"pi.form.nameRequired\"),\n          'input[name=\"name\"]',\n        );\n      }\n      if (!isEdit && !trimmedKey) {\n        throw new PiFormValidationError(\n          t(\"pi.form.providerKeyRequired\"),\n          \"#pi-provider-key\",\n        );\n      }","sourceCodeStart":1053,"sourceCodeEnd":1089,"githubUrl":"https://github.com/farion1231/cc-switch/blob/0b5da510168914b251481654a568c3ffacd62cf4/src/components/providers/forms/PiProviderForm.tsx#L1053-L1089","documentation":"PiProviderForm validates the 'settingsConfig' advanced JSON editor with parseJsonObject() before saving a Pi provider. If the text does not parse to a plain JSON object (it is an array, string, number, boolean, null, or syntactically invalid), submit() throws PiFormValidationError with jsonEditor.mustBeObject and focuses the element with id #pi-settings-config. Pi merges this object into its provider settings file, so a non-object top level would corrupt the config.","triggerScenarios":"Entering e.g. '[]', '\"custom\"', '123', or unbalanced JSON in the settings configuration editor and submitting; pasting a settings snippet that is a JSON array of objects instead of a single object.","commonSituations":"Copying a fragment from pi's settings.json that is an array-valued entry; leaving stray commas/quotes after hand-editing; wrapping config in brackets '[{...}]' out of habit.","solutions":["Make the editor content a single JSON object at the top level, e.g. '{ \"header\": { ... } }' instead of '[ ... ]'.","Run the text through JSON.parse in the console/devtools to find syntax errors, fix them, and resubmit.","If you meant to keep a list, nest it one level down as a named property of the root object."],"exampleFix":"// before\nsettingsConfig = \"[{ \\\"x\\\": 1 }]\"; // array at top level -> throws\n\n// after\nsettingsConfig = \"{ \\\"items\\\": [{ \\\"x\\\": 1 }] }\"; // object at top level","handlingStrategy":"validation","validationCode":"function isJsonObjectText(text: string): boolean {\n  try {\n    return JSON.parse(text) !== null && typeof JSON.parse(text) === \"object\" && !Array.isArray(JSON.parse(text));\n  } catch {\n    return false;\n  }\n}\n// before submit:\nif (!isJsonObjectText(settingsConfig)) setEditorError(\"Top level must be a JSON object\");","typeGuard":"function isPlainObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === \"object\" && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"catch (e) {\n  if (e instanceof PiFormValidationError) { setFormError(e.message); document.querySelector(\"#pi-settings-config\")?.focus(); return; }\n  throw e;\n}","preventionTips":["Validate JSON in the editor on blur with JSON.parse and show inline errors before submit.","When pasting config, check the first non-whitespace character is '{'."],"tags":["pi","form-validation","json","provider-config"],"backgroundTag":"schema-validation-failed","analyzedSha":"0b5da510168914b251481654a568c3ffacd62cf4","analyzedAt":"2026-08-20T14:29:00.113Z","contentChangedAt":"2026-08-20T14:29:00.113Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}