{"record":{"id":"843446e105ab4e8b","repo":"D4Vinci/Scrapling","slug":"invalid-json-data-json-string-err","errorCode":null,"errorMessage":"Invalid JSON data '{json_string}': {err}","messagePattern":"Invalid JSON data '(.+?)': (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"scrapling/cli.py","lineNumber":39,"sourceCode":"__OUTPUT_FILE_HELP__ = \"The output file path can be an HTML file, a Markdown file of the HTML content, or the text content itself. Use file extensions (`.html`/`.md`/`.txt`) respectively.\"\n__PACKAGE_DIR__ = Path(__file__).parent\n\n\ndef __Execute(cmd: List[str], help_line: str) -> None:  # pragma: no cover\n    print(f\"Installing {help_line}...\")\n    _ = check_output(cmd, shell=False)  # nosec B603\n    # I meant to not use try except here\n\n\ndef __ParseJSONData(json_string: Optional[str] = None) -> Optional[Dict[str, Any]]:\n    \"\"\"Parse JSON string into a Python object\"\"\"\n    if not json_string:\n        return None\n\n    try:\n        return json_loads(json_string)\n    except JSONDecodeError as err:  # pragma: no cover\n        raise ValueError(f\"Invalid JSON data '{json_string}': {err}\")\n\n\ndef __Request_and_Save(\n    fetcher_func: Callable[..., Response],\n    url: str,\n    output_file: str,\n    css_selector: Optional[str] = None,\n    ai_targeted: bool = False,\n    **kwargs,\n) -> None:\n    \"\"\"Make a request using the specified fetcher function and save the result\"\"\"\n    from scrapling.core.shell import Convertor\n\n    # Handle relative paths - convert to an absolute path based on the current working directory\n    output_path = Path(output_file)\n    if not output_path.is_absolute():\n        output_path = Path.cwd() / output_file\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/D4Vinci/Scrapling/blob/5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f/scrapling/cli.py#L21-L57","documentation":"The CLI's --json option expects a raw JSON string (an object of body parameters). __ParseJSONData runs orjson.loads on it and raises ValueError with the decoder message when the string is not valid JSON. This is strict input validation before the request is made; nothing is sent on the wire when it fires.","triggerScenarios":"Passing a non-JSON string to `scrapling fetch URL --json ...`, e.g. `--json 'a=b'`, `--json \"{key: 1}\"` (unquoted keys), or truncated JSON. Shell quoting mistakes (single vs double quotes around $variables) that corrupt the payload also land here.","commonSituations":"Building CLI commands in shell scripts where interpolation breaks quotes, hand-typing JSON without quoting keys, or passing form-encoded data (`k=v`) to the JSON flag instead of the params flag.","solutions":["Validate the payload locally first: `echo '<payload>' | python -m json.tool` (or `jq .`) to get the exact parse error","Quote JSON keys and the whole string: `--json '{\"key\": \"value\"}'`","Use single quotes around the JSON in the shell so $ and spaces survive; for data with single quotes, switch to a file or use jq to build it","If the data is key=value pairs, use the params option instead of --json"],"exampleFix":"# before\nscrapling fetch https://api.example.com --json \"username: test\"\n# ValueError: Invalid JSON data 'username: test': ...\n\n# after\nscrapling fetch https://api.example.com --json '{\"username\": \"test\"}'","handlingStrategy":"validation","validationCode":"import json\n\njson_str = '{\"key\": \"value\"}'\njson.loads(json_str)  # raises locally with a precise message before the CLI does","typeGuard":null,"tryCatchPattern":"try:\n    run_cli_fetch(url, json=payload)\nexcept ValueError as e:\n    if 'Invalid JSON data' in str(e):\n        payload = json.dumps(json.loads(payload))  # rebuild cleanly and retry once\n    else:\n        raise","preventionTips":["Build JSON payloads with json.dumps/jq instead of string concatenation","Single-quote the whole --json argument in the shell","Validate payload with a JSON linter before embedding in commands"],"tags":["cli","json","input-validation"],"backgroundTag":null,"analyzedSha":"5d213a2d4764002bfc4fed33c32fe09fa8b0bf7f","analyzedAt":"2026-08-14T22:23:09.440Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}