{"record":{"id":"6def6acd16baecf5","repo":"apify/crawlee","slug":"dataset-foreach-map-reduce-support-only-a-json","errorCode":null,"errorMessage":"Dataset.forEach/map/reduce() support only a \"json\" format.","messagePattern":"Dataset\\.forEach/map/reduce\\(\\) support only a \"json\" format\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storages/dataset.ts","lineNumber":544,"sourceCode":"     * **Example usage**\n     * ```javascript\n     * const dataset = await Dataset.open('my-results');\n     * await dataset.forEach(async (item, index) => {\n     *   console.log(`Item at ${index}: ${JSON.stringify(item)}`);\n     * });\n     * ```\n     *\n     * @param iteratee A function that is called for every item in the dataset.\n     * @param [options] All `forEach()` parameters.\n     * @param [index] Specifies the initial index number passed to the `iteratee` function.\n     * @default 0\n     */\n    async forEach(iteratee: DatasetConsumer<Data>, options: DatasetIteratorOptions = {}, index = 0): Promise<void> {\n        tryCancel();\n\n        if (!options.offset) options.offset = 0;\n        if (options.format && options.format !== 'json')\n            throw new Error('Dataset.forEach/map/reduce() support only a \"json\" format.');\n        if (!options.limit) options.limit = DATASET_ITERATORS_DEFAULT_LIMIT;\n\n        const { items, total, limit, offset } = await this.getData(options);\n\n        for (const item of items) {\n            await iteratee(item, index++);\n        }\n\n        const newOffset = offset + limit;\n        if (newOffset >= total) return;\n\n        const newOpts = { ...options, offset: newOffset };\n        await this.forEach(iteratee, newOpts, index);\n    }\n\n    /**\n     * Produces a new array of values by mapping each value in list through a transformation function `iteratee()`.\n     * Each invocation of `iteratee()` is called with two arguments: `(element, index)`.","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/storages/dataset.ts#L526-L562","documentation":"Dataset iteration helpers (forEach/map/reduce) only support the default 'json' format because they deserialize stored records into JavaScript objects for the iteratee callback. Requesting any other format (e.g. 'text', 'csv', 'binary') would break the typed item contract, so the library rejects it up front.","triggerScenarios":"Calling dataset.forEach/map/reduce (directly or via dataset.map(), reduce(), addFetchedRequests() with a text consumer, or makeText()) with an options object like { format: 'text' } or any format other than 'json'.","commonSituations":"Developers copying export/format options from getData() or writeFileToString() into forEach/map/reduce options; older code migrated from APIs that accepted a format option; attempting to read items as raw text inside a map() call.","solutions":["Remove the format option or set format: 'json' in the forEach/map/reduce options","If you need a non-JSON representation, fetch with dataset.getData({ format: 'text' }) or export via dataset.writeToJSONFile/writeToFileToString instead","Check the type of the iteratee you pass: makeText() internally calls forEach and must not receive a non-json format"],"exampleFix":"// before\nawait dataset.forEach(async (item) => { ... }, { format: 'text' });\n// after\nawait dataset.forEach(async (item) => { ... }); // format defaults to 'json'\nconst { items } = await dataset.getData({ format: 'text' }); // if you need raw text","handlingStrategy":"validation","validationCode":"function assertJsonFormat(options = {}) {\n  if (options.format && options.format !== 'json') {\n    throw new TypeError(`format '${options.format}' is not supported; use 'json'`);\n  }\n}\nassertJsonFormat({ format: 'text' }); // throws before calling forEach","typeGuard":"const isJsonFormat = (f) => f === undefined || f === 'json';","tryCatchPattern":null,"preventionTips":["Omit the format option entirely for forEach/map/reduce","Use getData()/writeToFile() for non-JSON formats","Keep iterator options limited to offset/limit"],"tags":["validation","dataset","api-misuse"],"backgroundTag":"unsupported-option-value","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}