# Util

## asJSONPrettyString

Convert an object to a pretty-printed JSON string.

```
Util.asJSONPrettyString(obj: Any) -> str
```

Parameters

```
obj: Object to convert.
```

Returns

```
A pretty-printed JSON string representation of the object.
```

## convertToResult

Convert a response to a Result object if possible.

```
Util.convertToResult(r) -> Result or None
```

Parameters

```
r: Response object to convert.
```

Returns

```
Result object if conversion is successful, otherwise None.
```

## asJSONString

Convert an object to a JSON string.

```
Util.asJSONString(obj: Any) -> str
```

Parameters

```
obj: Object to convert.
```

Returns

```
JSON string representation of the object.
```

## fromJSONString

Convert a JSON string to an object of the specified class.

```
Util.fromJSONString(json_str: str, cls: Any) -> Any
```

Parameters

```
json_str: JSON string to convert.
cls: Class of the object to create.
```

Returns

```
Object created from the JSON string
```

## fromJSONStringAsMap

Convert a JSON string to a dictionary.

```
Util.fromJSONStringAsMap(json_str: str) -> Dict[str, Any]
```

Parameters

```
json_str: JSON string to convert.
```

Returns

```
Dictionary created from the JSON string.
```

## saveAsJSONString

Save an object as JSON to a file.

```
Util.saveAsJSONString(file_path: str, obj: Any) -> None
```

Parameters

```
file_path: Path of the file to save.
obj: Object to save as JSON.
```

Returns

```
None.
```

## fromStringToDate

Convert a string to a datetime object.

```
Util.fromStringToDate(date, format="%Y-%m-%d %H:%M:%S") -> datetime
```

Parameters

```
date: String representing the date.
format: Format of the date string (default is "%Y-%m-%d %H:%M:%S").
```

Returns

```
Datetime object.
```

## fromDateToString

Convert a datetime object to a string.

```
Util.fromDateToString(date: datetime, format: str = "%Y-%m-%d %H:%M:%S") -> str
```

Parameters

```
date: Datetime object to convert.
format: Format of the output string (default is "%Y-%m-%d %H:%M:%S").
```

Returns

```
String representing the datetime.
```

## genRandomAlphaNum

Generate a random alphanumeric string of specified length.

```
Util.genRandomAlphaNum(length: int) -> str
```

Parameters

```
length: Length of the generated string.
```

Returns

```
Random alphanumeric string.
```

## mapByDisplayNames

Map objects by display names.

```
Util.mapByDisplayNames(input_map: Dict[str, Any], names: List[str], displayNames: List[str]) -> Dict[str, Any]
```

Parameters

```
input_map: Input map to process.
names: List of names.
displayNames: List of display names.
```

Returns

```
Mapped dictionary.
```

## listByDisplayNames

List objects by display names.

```
Util.listByDisplayNames(input_list: List[Dict[str, Any]], names: List[str], displayNames: List[str]) -> List[Dict[str, Any]]
```

Parameters

```
input_list: Input list to process.
names: List of names.
displayNames: List of display names.
```

Returns

```
List of dictionaries.
```

## deepCopy

Create a deep copy of a dictionary.

```
Util.deepCopy(input_map: Dict[str, Any]) -> Dict[str, Any]
```

Parameters

```
input_map: Input dictionary to copy.
```

Returns

```
Deep copy of the dictionary.
```

## convertToListOfDict

Convert input object to a list of dictionaries.

```
Util.convertToListOfDict(input_object) -> List[Dict[str, Any]]
```

Parameters

```
input_object: Input object to convert.
```

Returns

```
List of dictionaries.
```

## convertToList

Convert input object to a list.

```
Util.convertToList(input_object) -> List[Any]
```

Parameters

```
input_object: Input object to convert.
```

Returns

```
List.
```

## convertToInt

Convert input object to an integer.

```
Util.convertToInt(input_object) -> int
```

Parameters

```
input_object: Input object to convert.
```

Returns

```
Integer.
```

## convertToDict

Convert input object to a dictionary.

```
Util.convertToDict(input_object) -> Dict[str, Any]
```

Parameters

```
input_object: Input object to convert.
```

Returns

```
Dictionary.
```

## convertToBool

Convert input object to a boolean.

```
Util.convertToBool(input_object) -> bool
```

Parameters

```
input_object: Input object to convert.
```

Returns

```
Boolean.
```

## uidToClassName

Extract class name from UID.

```
Util.uidToClassName(uid) -> str or None
```

Parameters

```
uid: UID string.
```

Returns

```
Class name extracted from the UID.
```

## uidToId

Extract ID from UID.

```
Util.uidToId(uid) -> int
```

Parameters

```
uid: UID string.
```

Returns

```
ID extracted from the UID.
```

## uidToIdStr

Extract ID as string from UID.

```
Util.uidToIdStr(uid) -> str or None
```

Parameters

```
uid: UID string.
```

Returns

```
ID as string extracted from the UID.
```

## validateJson

Validate JSON string.

```
Util.validateJson(json_str) -> bool
```

Parameters

```
json_str: JSON string to validate.
```

Returns

```
True if valid JSON, False otherwise.
```

## toPrettyJSONString

Convert JSON string to pretty-printed JSON string.

```
Util.toPrettyJSONString(json_str) -> str
```

Parameters

```
json_str: JSON string to convert.
```

Returns

```
Pretty-printed JSON string.
```

## getCurrentLanguage

Get the current language.

```
Util.getCurrentLanguage() -> str
```

Parameters

```
None.
```

Returns

```
Current language.
```

## asUid

Convert class name and ID to UID.

```
Util.asUid(className, _id=None) -> str
```

Parameters

```
className: Class name.
_id: ID (optional).
```

Returns

```
UID.
```

## addObjectByPath

Add object to dictionary by path.

```
Util.addObjectByPath(map_, path, value)
```

Parameters

```
map_: Dictionary to add the object to.
path: Path to the object.
value: Object to add.
```

Returns

```
None.
```

## removeLeadingChars

Remove leading characters from a string.

```
Util.removeLeadingChars(s, c) -> str
```

Parameters

```
s: Input string.
c: Character to remove.
```

Returns

```
String with leading characters removed.
```

## removeLastCharIfMatches

Remove last character from a string if it matches a specified character.

```
Util.removeLastCharIfMatches(s, c) -> str
```

Parameters

```
s: Input string.
c: Character to match.
```

Returns

```
String with last character removed if it matches the specified character.
```

## roundDouble

Round a floating-point number to specified decimal places.

```
Util.roundDouble(value: float, places: int) -> float
```

Parameters

```
value: Floating-point number to round.
places: Number of decimal places.
```

Returns

```
Rounded floating-point number.
```

## urlEncode

URL-encode a string.

```
Util.urlEncode(s: str) -> str
```

Parameters

```
s: String to encode.
```

Returns

```
URL-encoded string.
```

## urlDecode

URL-decode a string.

```
Util.urlDecode(s: str) -> str
```

Parameters

```
s: String to decode.
```

Returns

```
URL-decoded string.
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trillo.gitbook.io/trillo-workbench-python-sdk/util.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
