# BaseApi (Basic APIs)

## asJSONPrettyString

Transform an object into a neatly formatted JSON string.

```python
BaseApi.asJSONPrettyString(obj)
```

**Parameters**

{% code overflow="wrap" %}

```
obj: the object that needs to be converted to JSON string (can be a dict)
```

{% endcode %}

**Sample Code**

```python
# Create a dictionary to hold product information
product = {"productId": "11"}
str_json = BaseApi.asJSONPrettyString(product)
```

**Returns**

{% code overflow="wrap" %}

```
This endpoint will return the formatted string of the dictionary, with indentation         and sorted keys, for better readability and understanding.
```

{% endcode %}

## asJSONString

Transform an object into a non-formatted JSON string.

```python
BaseApi.asJSONString(obj)
```

**Parameters**

{% code overflow="wrap" %}

```
obj:  the object that needs to be converted to JSON string (can be a dict)
```

{% endcode %}

**Sample Code**

```python
product = {
    "productId": "11"
}
str_json = BaseApi.asJSONString(product)
```

**Returns**

```
This method returns the string representation of the object in JSON format, without formatting, for improved readability and comprehension.
```

## fromJSONString

Parse a JSON-formatted string and convert it into a Map object.

```
BaseApi.fromJSONString(jsonString)
```

**Parameters**

```
jsonString:     A string representing a valid JSON structure.
```

**Sample Code**

```python
jsonString = "{\n" + \
            "    \"productId\": \"abc\"\n" + \
            "}"
map = BaseApi.fromJSONString(jsonString)
```

**Returns**

{% code overflow="wrap" %}

```
A constructed dict represents the key-value pairs extracted from the provided JSON string.
```

{% endcode %}

## fromJSONStringAsArray

Convert a json string​ as a dictionary.

```
BaseApi.fromJSONStringAsArray(jsonString)
```

**Parameters**

```
jsonString:    string representation of json
```

**Sample Code**

```python
json_string = '[\n' + \
    '    {\n' + \
    '    "product": "abc"\n' + \
    '    }\n' + \
    ']'

mapList = BaseApi.fromJSONStringAsArray(jsonString)
```

**Returns**

```
the array of json objects
```

## readJSON

Read the JSON from a file name​ located in a specific directory. It is converted into a dictionary

```
BaseApi.readJSON(dir, fileName)
```

**Parameters**

```
dir:    the directory as str
fileName:  name of the file as str
```

**Sample Code**

```python
map = BaseApi.readJSON('/tmp/folder1', 'file.json')
```

**Returns**

```
json document object​ as dictionary
```

## writeJSON

Write a dictionary object into a file​ located on a specific directory.

```
BaseApi.writeJSON(dir, fileName, obj)
```

**Parameters**

```
dir:         directory as str
fileName:    name of the file as str
obj:         json document object 
```

**Sample Code**

```python
json_string = '[\n' + \
    '    {\n' + \
    '    "product": "abc"\n' + \
    '    }\n' + \
    ']'

BaseApi.writeJSON("/tmp/folder1", "file.json", json_string)
```

**Returns**

```
result of the operation​
```

## writePrettyJSON

Write a dictionary object into a file​ located on a specific directory. The JSON information is a formatted inside the file.

```
BaseApi.writePrettyJSON(dir, fileName, obj)
```

**Parameters**

```
dir:          directory as str
fileName:     name of the file as str
obj:          json formatted document
```

**Sample Code**

```python
json_string = '[\n' + \
    '    {\n' + \
    '    "product": "abc"\n' + \
    '    }\n' + \
    ']'

BaseApi.writePrettyJSON('/tmp/folder1', 'file.json', jsonString)
```

**Returns**

```
result of the operation​
```

## successResult

Build and return a successful result dictionary object.​

```
BaseApi.successResult()
or
BaseApi.successResult(msg)
or
BaseApi.successResult(msg, data)
```

**Parameters**

```
msg  :  message to be sent as str
data :  Object
```

**Sample Code**

```python
BaseApi.successResult('Successfully added records')
```

**Returns**

```
This method returns a success Result object. If provided, the message and data are encapsulated within the result.
```

## errorResult

Build and return an error result dictionary object.​

```
BaseApi.errorResult(msg)
or
BaseApi.errorResult(msg, code)
```

**Parameters**

```
msg:     message to be sent (str)
code:    return code (int)
```

**Sample Code**

```python
BaseApi.errorResult('Failed to process records')
```

**Returns**

```
This method returns an error Result object with the specified message and, optionally, a code.
```

## extractMessage

Extract the message from the result object​. The result object has a detailed message in it therefore it will be returned.

```
BaseApi.extractMessage(obj)
```

**Parameters**

```
obj: object as a dictionary
```

**Returns**

```
This method extracts the message from a Result object if the object is a Result; otherwise, it returns "Unknown".
```

## isResult

Checking whether the object is a result object​.

```
BaseApi.isResult(obj)
```

**Parameters**

```
obj:    the object as a dictionary
```

**Sample Code**

```python
res = Result.getSuccessResult('success')
isResult = BaseApi.isResult(res)
```

**Returns**

```
This method returns True if the provided object is an instance of Result; otherwise, it returns False.
```

## isResultOrNull

Checking if result object is a valid one or a just a NuLL

```
BaseApi.isResultOrNull(obj)
```

**Parameters**

```
obj:    the object as a dictionary
```

**Sample Code**

```python
res = Result.getSuccessResult('success')
isResult = BaseApi.isResultOrNull(res)
```

**Returns**

```
This method returns True if the provided object is either an instance of Result or None; otherwise, it returns False.
```

## UUID

Generate a universally unique identifier (UUID) string.

```
BaseApi.UUID()
```

**Parameters**

```
none​
```

**Sample Code**

```python
uuid = BaseApi.UUID()
```

**Returns**

```
This method returns a string representing a randomly generated UUID.
```

## uidToClassName

Extract class name​ from an input string.

```
BaseApi.uidToClassName(uid)
```

**Parameters**

```
uid:    the className id as str
```

**Returns**

```
This method returns a class name string derived from the provided UID.
```

## uidToId

Extract an long id number from a string UID.

```
BaseApi.uidToId(uid)
```

**Parameters**

```
uid: str
```

**Returns**

```
This method returns an integer ID extracted from the provided UID string.
```

## uidToIdStr

Retrieves​ the ID as a string from uid.

```
BaseApi.uidToIdStr(uid) 
```

**Parameters**

```
uid: str
```

**Returns**

```
This method returns a string representation of the ID extracted from the provided UID.
```

## waitForMillis

Pause execution for the specified number of milliseconds.

```
BaseApi.waitForMillis(tm) 
```

**Parameters**

```
tm: long
```

**Sample Code**

```python
res = BaseApi.waitForMillis(10)
```

**Returns**

```
This method returns a Result indicating the success or failure of the wait operation.
```

## remoteCall

Perform a remote method call.&#x20;

<pre><code><strong>BaseApi.remoteCall(javaClassName, javaMethodName, *args)
</strong></code></pre>

**Parameters**

```
javaClassName: str
javaMethodName: str
```

**Sample Code**

```python
res = remoteCall('DSApi', 'get', 'shared.common.product', '12')
```

**Returns**

```
This method returns the result of the remote method call.
```

## remoteCallAsResult

Perform a remote method call and return the result as a Result object.

```
BaseApi.remoteCallAsResult(javaClassName, javaMethodName, *args)
```

#### Parameters

```
javaClassName: str
javaMethodName: str
```

**Sample Code**

```python
res = remoteCallAsResult('BigQueryApi', 'executeQuery', 'SELECT ID FROM product_tbl')
```

**Returns**

```
This method returns the result of the remote method call encapsulated within a Result object.
```

## remoteCallAsMap

Perform a remote method call and return the result as a dictionary.

```
BaseApi.remoteCallAsMap(javaClassName, javaMethodName, *args)
```

#### Parameters

```
javaClassName: str
javaMethodName: str
```

**Sample Code**

```python
res =  remoteCallAsMap('UMApi', 'getCurrentUser')
```

**Returns**

```
This method returns the result of the remote method call as a dictionary.
```

## remoteCallAsString

Perform a remote method call and return the result as a string.

```
BaseApi.remoteCallAsString(javaClassName, javaMethodName, *args)
```

#### Parameters

```
javaClassName: str
javaMethodName: str
```

**Sample Code**

```python
res = remoteCallAsString('GCPRestApi', 'getProjectId')
```

**Returns**

```
This method returns the result of the remote method call as a string.
```

## remoteCallAsList

Perform a remote method call and return the result as a list.

```
BaseApi.remoteCallAsList(javaClassName, javaMethodName, *args)
```

#### Parameters

```
javaClassName: str
javaMethodName: str
```

#### Returns

```
This method returns the result of the remote method call as a list.
```

## remoteCallAsListOfMaps

Perform a remote method call and return the result as a list of dictionaries.

```
BaseApi.remoteCallAsListOfMaps(javaClassName, javaMethodName, *args)
```

#### Parameters

```
javaClassName: str
javaMethodName: str
```

#### Returns

```
This method returns the result of the remote method call as a list of dictionaries.
```


---

# 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/json.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.
