ansible/ansible · error · ValueError

No start of json char found

Error message

No start of json char found

What it means

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.

Source

Thrown at lib/ansible/module_utils/json_utils.py:56

    Filters leading lines before first line-starting occurrence of '{' or '[', and filter all
    trailing lines after matching close character (working from the bottom of output).
    """
    warnings = []

    # Filter initial junk
    lines = data.splitlines()

    for start, line in enumerate(lines):
        line = line.strip()
        if line.startswith(u'{'):
            endchar = u'}'
            break
        elif not objects_only and line.startswith(u'['):
            endchar = u']'
            break
    else:
        raise ValueError('No start of json char found')

    # Filter trailing junk
    lines = lines[start:]

    for reverse_end_offset, line in enumerate(reversed(lines)):
        if line.strip().endswith(endchar):
            break
    else:
        raise ValueError('No end of json char found')

    if reverse_end_offset > 0:
        # Trailing junk is uncommon and can point to things the user might
        # want to change.  So print a warning if we find any
        trailing_junk = lines[len(lines) - reverse_end_offset:]
        for line in trailing_junk:
            if line.strip():
                warnings.append('Module invocation had junk after the JSON data: %s' % '\n'.join(trailing_junk))
                break

View on GitHub (pinned to 9cf16a4aca)

Solutions

  1. Inspect the raw output you are parsing (debug/print it) and confirm it actually contains a line starting with '{' or '['.
  2. Strip non-JSON preamble so the JSON starts at the beginning of a line before parsing (the filter only skips whole junk lines).
  3. 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).
  4. Handle device error output first: check return code / error text instead of assuming JSON.

Example fix

# before
- ansible.builtin.command: some-cli --json
  register: out
- ansible.builtin.set_fact:
    data: "{{ out.stdout | from_json }}"   # fails when stdout has no JSON line
# after
- ansible.builtin.command: some-cli --json
  register: out
- ansible.builtin.set_fact:
    data: "{{ (out.stdout | regex_search('(?s)\{.*\}') | from_json) }}"
Defensive patterns

Strategy: validation

Validate before calling

data = stdout.strip()
has_json_start = any(line.strip().startswith(('{', '[')) for line in data.splitlines())
if not has_json_start:
    raise ValueError(f'Output contains no JSON document: {data[:200]!r}')

Try / catch

try:
    result = filter_json(data)
except ValueError:
    # handle non-JSON output: treat as failure text
    result = None
    error_text = data

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Related errors


AI-assisted analysis of ansible/ansible@9cf16a4aca (2026-08-15). Data as JSON: /api/errors/a6edef7457565d48. Report an issue: GitHub.