Skip to content

datasource.readFile ^v2.7.191 ​

Reads a file, as text, lines, parsed JSON or XML, or base64.

js
datasource.readFile(filePath, readAs, encoding)

Parameters ​

ParameterTypeDescription
filePathstringThe path of the file.
readAsstringOptional. How to return the file; see the table below. Defaults to "string".
encodingstringOptional. The character encoding, such as "UTF-8" or "ISO-8859-1". Detected automatically when left out or "auto".

Returns ​

any

The file in the form readAs asks for, or false when the path is invalid.

readAsReturns
"string"The whole file as one string.
"lines"An array of strings, one per line.
"json"The file parsed as JSON.
"xml"The file parsed as XML.
"base64"The file's contents, base64-encoded.

Examples ​

js
// Read file as a string (default)
const text = datasource.readFile("input/report.txt");

// Read file as an array of lines
const lines = datasource.readFile("input/data.txt", "lines");
for (const line of lines) {
    console.log(line);
}

// Read and parse a JSON file
const config = datasource.readFile("input/config.json", "json");
console.log(config.name);

// Read and parse an XML file
const xmlData = datasource.readFile("input/data.xml", "xml");

// Read a binary file as Base64
const base64 = datasource.readFile("input/image.png", "base64");

// Read a file with specific encoding
const legacyData = datasource.readFile("input/legacy.txt", "string", "ISO-8859-1");