{"record":{"id":"a6edef7457565d48","repo":"ansible/ansible","slug":"no-start-of-json-char-found","errorCode":null,"errorMessage":"No start of json char found","messagePattern":"No start of json char found","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/ansible/module_utils/json_utils.py","lineNumber":56,"sourceCode":"\n    Filters leading lines before first line-starting occurrence of '{' or '[', and filter all\n    trailing lines after matching close character (working from the bottom of output).\n    \"\"\"\n    warnings = []\n\n    # Filter initial junk\n    lines = data.splitlines()\n\n    for start, line in enumerate(lines):\n        line = line.strip()\n        if line.startswith(u'{'):\n            endchar = u'}'\n            break\n        elif not objects_only and line.startswith(u'['):\n            endchar = u']'\n            break\n    else:\n        raise ValueError('No start of json char found')\n\n    # Filter trailing junk\n    lines = lines[start:]\n\n    for reverse_end_offset, line in enumerate(reversed(lines)):\n        if line.strip().endswith(endchar):\n            break\n    else:\n        raise ValueError('No end of json char found')\n\n    if reverse_end_offset > 0:\n        # Trailing junk is uncommon and can point to things the user might\n        # want to change.  So print a warning if we find any\n        trailing_junk = lines[len(lines) - reverse_end_offset:]\n        for line in trailing_junk:\n            if line.strip():\n                warnings.append('Module invocation had junk after the JSON data: %s' % '\\n'.join(trailing_junk))\n                break","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/ansible/ansible/blob/9cf16a4aca7898481c257f1e17ad28d0b67b1f85/lib/ansible/module_utils/json_utils.py#L38-L74","documentation":"Raised by ansible.module_utils.json_utils (the module-output junk filter used when parsing a module's stdout as JSON): after splitting the data into lines and skipping leading junk, no line ever began with '{' (or '[' when objects_only=False), so there is no JSON document to extract. It means the producer's output contained no JSON object/array start at all, not merely noise around it.","triggerScenarios":"Calling the filter (used internally when a module or shell command's output is expected to be JSON-with-junk) where output is empty, purely textual (e.g. an error banner, 'Traceback ...', login prompts), or where the JSON starts mid-line rather than at a line start after strip().","commonSituations":"command/shell tasks piping device CLI output expected to be JSON but the device printed an error; SSH banner or MOTD polluting output; module crashed before printing JSON; nested quotes shifting the payload so '{' is not at line start.","solutions":["Inspect the raw output you are parsing (debug/print it) and confirm it actually contains a line starting with '{' or '['.","Strip non-JSON preamble so the JSON starts at the beginning of a line before parsing (the filter only skips whole junk lines).","Fix the producer: for command modules, ensure the command prints pure JSON, or use stdout filtering (e.g. from_json after selecting the JSON line).","Handle device error output first: check return code / error text instead of assuming JSON."],"exampleFix":"# before\n- ansible.builtin.command: some-cli --json\n  register: out\n- ansible.builtin.set_fact:\n    data: \"{{ out.stdout | from_json }}\"   # fails when stdout has no JSON line\n# after\n- ansible.builtin.command: some-cli --json\n  register: out\n- ansible.builtin.set_fact:\n    data: \"{{ (out.stdout | regex_search('(?s)\\{.*\\}') | from_json) }}\"","handlingStrategy":"validation","validationCode":"data = stdout.strip()\nhas_json_start = any(line.strip().startswith(('{', '[')) for line in data.splitlines())\nif not has_json_start:\n    raise ValueError(f'Output contains no JSON document: {data[:200]!r}')","typeGuard":null,"tryCatchPattern":"try:\n    result = filter_json(data)\nexcept ValueError:\n    # handle non-JSON output: treat as failure text\n    result = None\n    error_text = data","preventionTips":["Check return codes and error output before assuming JSON from commands.","Use from_json only after confirming output shape; debug-print raw stdout when parsing fails.","Force machine-readable output flags on CLIs (--json, | display json) when available."],"tags":["json","parsing","stdout","ansible"],"backgroundTag":null,"analyzedSha":"9cf16a4aca7898481c257f1e17ad28d0b67b1f85","analyzedAt":"2026-08-15T00:15:47.100Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}