# Buggy code
def parse_commands(module, warnings):
    spec = dict(
        command=dict(key=True),
        output=dict(default=module.params["display"], choices=["text", "json"]),
        prompt=dict(),
        response=dict(),
    )

    transform = ComplexList(spec, module)
    commands = transform(module.params["commands"])

    for index, item in enumerate(commands):
        if module.check_mode and not item["command"].startswith("show"):
            warnings.append(
                "Only show commands are supported when using check_mode, not "
                "executing %s" % item["command"]
            )

        if item["output"] == "json" and "display json" not in item["command"]:
            item["command"] += "| display json"
        elif item["output"] == "text" and "display json" in item["command"]:
            item["command"] = item["command"].replace("| display json", "")

        commands[index] = item

    return commands


# Traceback (ValueError)
[WARNING]: Removed unexpected internal key in module return: _ansible_parsed = False

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_CHppt1/ansible_module_junos_command.py", line 250, in <module>
main()
File "/tmp/ansible_CHppt1/ansible_module_junos_command.py", line 208, in main
commands = parse_commands(module, warnings)
File "/tmp/ansible_CHppt1/ansible_module_junos_command.py", line 164, in parse_commands
commands = transform(module.params['commands'])
File "/tmp/ansible_CHppt1/ansible_modlib.zip/ansible/module_utils/network_common.py", line 150, in __call__
File "/tmp/ansible_CHppt1/ansible_modlib.zip/ansible/module_utils/network_common.py", line 135, in __call__
ValueError: output must be one of text, json, got xml

fatal: [vsrx01]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_CHppt1/ansible_module_junos_command.py\", line 250, in <module>\n    main()\n  File \"/tmp/ansible_CHppt1/ansible_module_junos_command.py\", line 208, in main\n    commands = parse_commands(module, warnings)\n  File \"/tmp/ansible_CHppt1/ansible_module_junos_command.py\", line 164, in parse_commands\n    commands = transform(module.params['commands'])\n  File \"/tmp/ansible_CHppt1/ansible_modlib.zip/ansible/module_utils/network_common.py\", line 150, in __call__\n  File \"/tmp/ansible_CHppt1/ansible_modlib.zip/ansible/module_utils/network_common.py\", line 135, in __call__\nValueError: output must be one of text, json, got xml\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def parse_commands(module, warnings):
    spec = dict(
        command=dict(key=True),
        output=dict(default=module.params["display"], choices=["text", "json", "xml"]),
        prompt=dict(),
        answer=dict(),
    )

    transform = ComplexList(spec, module)
    commands = transform(module.params["commands"])

    for index, item in enumerate(commands):
        if module.check_mode and not item["command"].startswith("show"):
            warnings.append(
                "Only show commands are supported when using check_mode, not "
                "executing %s" % item["command"]
            )

        if item["output"] == "json" and "display json" not in item["command"]:
            item["command"] += "| display json"
        elif item["output"] == "xml" and "display xml" not in item["command"]:
            item["command"] += "| display xml"
        else:
            if "| display json" in item["command"]:
                item["command"] = str(item["command"]).replace(" | display json", "")
            elif "| display xml" in item["command"]:
                item["command"] = str(item["command"]).replace(" | display xml", "")
        commands[index] = item

    return commands