def export_to_json(
    table, filename_or_fobj=None, encoding="utf-8", indent=None, *args, **kwargs
):
    """Export a `rows.Table` to a JSON file or file-like object.

    If a file-like object is provided it MUST be open in binary mode (like in
    `open('myfile.json', mode='wb')`).
    """
    # TODO: will work only if table.fields is OrderedDict

    fields = table.fields
    prepared_table = prepare_to_export(table, *args, **kwargs)
    field_names = next(prepared_table)
    data = [
        {
            field_name: _convert(value, fields[field_name], *args, **kwargs)
            for field_name, value in zip(field_names, row)
        }
        for row in prepared_table
    ]

    result = json.dumps(data, indent=indent)
    if type(result) is six.text_type:  # Python 3
        result = result.encode(encoding)

    if indent is not None:
        # clean up empty spaces at the end of lines
        result = b"\n".join(line.rstrip() for line in result.splitlines())

    return export_data(filename_or_fobj, result, mode="wb")
Export a `rows.Table` to a JSON file or file-like object.

    If a file-like object is provided it MUST be open in binary mode (like in
    `open('myfile.json', mode='wb')`).