{"record":{"id":"0e69cab5636de324","repo":"HumanSignal/label-studio","slug":"failed-to-parse-json-file-self-file-name-extra","errorCode":null,"errorMessage":"Failed to parse JSON file {self.file_name}: {extract_message(exc)}","messagePattern":"Failed to parse JSON file (.+?): (.+?)","errorType":"validation","errorClass":"ValidationError","httpStatus":400,"severity":"error","filePath":"label_studio/data_import/models.py","lineNumber":218,"sourceCode":"                    # Single JSON object: parse once and yield a single-item batch\n                    raw_data = file_handle.read()\n                    try:\n                        task_data = json.loads(raw_data)\n                    except TypeError:\n                        task_data = json.loads(raw_data.decode('utf8'))\n                    formatted_task = self._format_task_for_json_streaming(task_data)\n                    batch.append(formatted_task)\n\n                else:\n                    # Unknown/invalid JSON structure\n                    raise ValidationError('Unsupported or invalid JSON structure')\n\n                # Yield remaining tasks if any\n                if batch:\n                    yield batch\n\n        except Exception as exc:\n            raise ValidationError(f'Failed to parse JSON file {self.file_name}: {extract_message(exc)}')\n\n    def _format_task_for_json_streaming(self, task):\n        \"\"\"Format task data for JSON streaming consistency with read_tasks_list_from_json\"\"\"\n        # Handle different task types as in the original read_tasks_list_from_json method\n        if isinstance(task, dict):\n            if not task.get('data'):\n                task = {'data': task}\n        else:\n            # If task is not a dict (e.g., list), wrap it in {'data': task}\n            task = {'data': task}\n\n        if not isinstance(task['data'], dict):\n            raise ValidationError('Task item should be dict')\n        return task\n\n    def read_task_from_hypertext_body(self):\n        logger.debug('Read 1 task from hypertext file {}'.format(self.filepath))\n        body = self.content","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/HumanSignal/label-studio/blob/0b49e9b53917880baf1dd85d574fe5541a9aafb2/label_studio/data_import/models.py#L200-L236","documentation":"read_tasks_list_from_json_streaming wraps any exception raised while parsing/iterating the JSON file into a single ValidationError that includes the file name and the underlying message via extract_message(exc). It is a catch-all transformer, so the root cause (JSONDecodeError, the 'Unsupported or invalid JSON structure' error, encoding errors, etc.) appears in the message text.","triggerScenarios":"Any exception inside the streaming read: invalid JSON syntax (json.loads failure), unsupported top-level structure, decode errors on non-UTF8 bytes, or I/O errors reading the file during read_tasks_streaming.","commonSituations":"Truncated or corrupted upload; file saved with wrong encoding (UTF-16); JSON with trailing commas or comments; uploading a CSV renamed to .json.","solutions":["Read the wrapped inner message after 'Failed to parse JSON file ...:' and fix the underlying JSON syntax issue","Run the file through a JSON linter / python -m json.tool to locate the syntax error","Re-save the file as UTF-8 and as valid JSON (no trailing commas, comments, or envelope objects)"],"exampleFix":"// before: file contains BOM/UTF-16 or trailing comma\n{\"a\": 1,}\n// after: valid UTF-8 JSON\n{\"a\": 1}","handlingStrategy":"try-catch","validationCode":"import json\nraw = open('tasks.json', 'rb').read()\ntext = raw.decode('utf8')  # raises on wrong encoding\njson.loads(text)           # raises with position on bad syntax","typeGuard":null,"tryCatchPattern":"try:\n    for batch in fu.read_tasks_streaming():\n        yield batch\nexcept ValidationError as e:\n    # e contains 'Failed to parse JSON file <name>: <inner cause>'\n    raise ValueError(f\"Fix JSON before import: {e}\") from e","preventionTips":["Always save as UTF-8 without BOM","Run python -m json.tool on the file before upload","Never rename CSV/TSV files to .json"],"tags":["json","parsing","streaming","data-import"],"backgroundTag":"json-parse-error","analyzedSha":"0b49e9b53917880baf1dd85d574fe5541a9aafb2","analyzedAt":"2026-08-29T00:39:52.578Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}