{"record":{"id":"ae204d53f5ed0181","repo":"payloadcms/payload","slug":"invalid-json-format","errorCode":null,"errorMessage":"Invalid JSON format","messagePattern":"Invalid JSON format","errorType":"validation","errorClass":"APIError","httpStatus":500,"severity":"error","filePath":"packages/plugin-import-export/src/utilities/parseJSON.ts","lineNumber":29,"sourceCode":" * Parses JSON data into an array of record objects.\n * Validates that the input is an array of documents.\n */\nexport const parseJSON = ({ data, req }: ParseJSONArgs): Record<string, unknown>[] => {\n  try {\n    const content = typeof data === 'string' ? data : data.toString('utf-8')\n    const parsed = JSON.parse(content)\n\n    if (!Array.isArray(parsed)) {\n      throw new APIError('JSON import data must be an array of documents')\n    }\n\n    return parsed\n  } catch (err) {\n    req.payload.logger.error({ err, msg: 'Error parsing JSON' })\n    if (err instanceof APIError) {\n      throw err\n    }\n    throw new APIError('Invalid JSON format')\n  }\n}\n","sourceCodeStart":11,"sourceCodeEnd":32,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/plugin-import-export/src/utilities/parseJSON.ts#L11-L32","documentation":"parseJSON wraps JSON.parse in try/catch. If parsing throws a SyntaxError (and it isn't an APIError), it logs the error and rethrows as APIError 'Invalid JSON format'. The bytes were not valid JSON.","triggerScenarios":"Malformed JSON: trailing comma, unquoted keys, BOM, truncated file, or a non-JSON file (e.g. CSV) fed in with format='json'.","commonSituations":"format field mismatch (CSV content sent as json); encoding issues (UTF-16/BOM); hand-edited file with a syntax error; truncated upload.","solutions":["Validate the content parses with JSON.parse before import and show the parse error.","Confirm the format field matches the actual file content.","Re-encode the file as UTF-8 without BOM if needed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"let content: string\ntry {\n  content = typeof data === 'string' ? data : data.toString('utf-8')\n  JSON.parse(content) // pre-validate, surface real syntax error\n} catch (e) {\n  throw new Error(`Source is not valid JSON: ${(e as Error).message}`)\n}","typeGuard":null,"tryCatchPattern":"try {\n  await createImport({ ..., format: 'json' })\n} catch (err) {\n  if (err instanceof APIError && /Invalid JSON format/.test(err.message)) {\n    // tell the user the file isn't valid JSON or format is wrong\n  } else throw err\n}","preventionTips":["Match the format field to the actual file content (csv vs json).","Re-encode files as UTF-8 without BOM before import.","Lint the JSON in the upload UI before queueing the import."],"tags":["json","import","validation","plugin-import-export"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}