Export Functions
The UniFi Controller API includes utilities for exporting data to various formats.
Functions for exporting UniFi device information to various formats.
This module provides simple export utilities for UniFi device data, allowing export to CSV, JSON, and Python dictionaries without external dependencies.
- class unifi_controller_api.export.UnifiEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
JSONEncoder
Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.
If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.
If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (’, ‘, ‘: ‘) if indent is
None
and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a
TypeError
.- default(obj)[source]
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return super().default(o)
- unifi_controller_api.export.to_dict_list(items)[source]
Convert a list of UniFi model objects to a list of dictionaries.
This is useful for further processing or serialization.
- unifi_controller_api.export.export_csv(items, path, fields=None, flatten_nested=False)[source]
Export UniFi objects to a CSV file.
- Parameters:
items (
List
[TypeVar
(T
,UnifiDevice
,UnifiSite
)]) – List of UniFi model objects (UnifiDevice or UnifiSite)path (
str
) – Path where the CSV file will be savedfields (
Optional
[List
[str
]]) – Optional list of specific fields to include in the export. If not provided, all fields will be exported.flatten_nested (
bool
) – Whether to flatten nested structures using dot notation (default: False) For example, uplink.type becomes uplink_type
- Return type:
- unifi_controller_api.export.export_json(items, path, indent=2)[source]
Export UniFi objects to a JSON file.
Supported Export Formats
The API supports exporting data to the following formats:
CSV - Comma-separated values format for spreadsheets
JSON - JSON format for machine-readable data
YAML - YAML format for human-readable data
Usage Examples
Exporting to CSV
from unifi_controller_api import UnifiController
from unifi_controller_api.export import export_to_csv
# Get devices
controller = UnifiController("https://unifi.example.com", "admin", "password")
devices = controller.get_unifi_site_device("default")
# Export to CSV
export_to_csv(devices, "devices.csv")
Exporting to JSON
from unifi_controller_api import UnifiController
from unifi_controller_api.export import export_to_json
# Get clients
controller = UnifiController("https://unifi.example.com", "admin", "password")
clients = controller.get_unifi_site_client("default")
# Export to JSON
export_to_json(clients, "clients.json")