*Outputs JSON Schema to terminal or a file.*

        By default, the schema is output for the last request and response.

        The output can be limited further by:

        - The property of the last instance, e.g. ``request`` or ``response``
        - Any nested property that exists, similarly as for assertion keywords

        Also variables and values that can be converted to JSON are accepted,
        in which case the schema is generated for those instead.

        *Options*

        ``file_path``: The JSON Schema is written to a file instead of terminal.
        The file is created if it does not exist.

        ``append``: If true, the JSON Schema is appended to the given file
        instead of truncating it first.

        ``sort_keys``: If true, the JSON Schema is sorted alphabetically by
        property names before it is output.

        *Examples*

        | `Output Schema` | response | ${CURDIR}/response_schema.json | # Write a file to use with `Expect Response` |
        | `Output Schema` | response body | ${CURDIR}/response_body_schema.json | # Write a file to use with `Expect Response Body` |

        | `Output Schema` | $.email | # only the schema for one response body property |
        | `Output Schema` | $..geo | # only the schema for the nested response body property |

def output_schema(
        self, what="", file_path=None, append=False, sort_keys=False
    ):
        """*Outputs JSON Schema to terminal or a file.*

        By default, the schema is output for the last request and response.

        The output can be limited further by:

        - The property of the last instance, e.g. ``request`` or ``response``
        - Any nested property that exists, similarly as for assertion keywords

        Also variables and values that can be converted to JSON are accepted,
        in which case the schema is generated for those instead.

        *Options*

        ``file_path``: The JSON Schema is written to a file instead of terminal.
        The file is created if it does not exist.

        ``append``: If true, the JSON Schema is appended to the given file
        instead of truncating it first.

        ``sort_keys``: If true, the JSON Schema is sorted alphabetically by
        property names before it is output.

        *Examples*

        | `Output Schema` | response | ${CURDIR}/response_schema.json | # Write a file to use with `Expect Response` |
        | `Output Schema` | response body | ${CURDIR}/response_body_schema.json | # Write a file to use with `Expect Response Body` |

        | `Output Schema` | $.email | # only the schema for one response body property |
        | `Output Schema` | $..geo | # only the schema for the nested response body property |
        """
        if isinstance(what, (STRING_TYPES)):
            if what == "":
                try:
                    json = self._last_instance_or_error()["schema"]
                except IndexError:
                    raise RuntimeError(no_instances_error)
            elif what.startswith(("request", "response", "$")):
                self._last_instance_or_error()
                matches = self._find_by_field(what)
                if len(matches) > 1:
                    json = [found["schema"] for found in matches]
                else:
                    json = matches[0]["schema"]
            else:
                try:
                    json = self._new_schema(self._input_json_as_string(what))
                except ValueError:
                    json = self._new_schema(self._input_string(what))
        else:
            json = self._new_schema(self._input_json_from_non_string(what))
        sort_keys = self._input_boolean(sort_keys)
        if not file_path:
            self.log_json(json, sort_keys=sort_keys)
        else:
            content = dumps(
                json,
                ensure_ascii=False,
                indent=4,
                separators=(",", ": "),
                sort_keys=sort_keys,
            )
            write_mode = "a" if self._input_boolean(append) else "w"
            try:
                with open(
                    path.join(getcwd(), file_path), write_mode, encoding="utf-8"
                ) as file:
                    if IS_PYTHON_2:
                        content = unicode(content)
                    file.write(content)
            except IOError as e:
                raise RuntimeError(
                    "Error outputting to file '%s':\n%s" % (file_path, e)
                )
        return json