Log In    |    Knowledge Base    |    4D Home
Tech Tip: Converting Arrays to a String
Published On: October 31, 2023

Arrays are useful when trying to contain a set of data. However, when trying to interact and view the data, it may not be as simple due to some of the limitations of arrays in the debugger.
In the debugger, an array will only display a max number of 100 elements. Also the entire array cannot easily be exported to inspect. As such, it can be easier to convert the array to a string format such a comma separated values. A string in the debugger can easily be copied and pasted into a text editor or exported using one of the many 4D features.

Below are some ways to convert an Array to a String:

- The JSON Stringify array command. This command takes in an array as a parameter and then outputs it in a string as a JSON serialized array format.
The format list each element in JSON format separated by commas. The entire array is held in square brackets [].

$varText:= JSON Stringify array($textArray)

With the example code above, if $textArray contained the three elements {"a", "b", "c"} the results in $varText would be
"[\"a\", \"b\",\"c\"]"

- Converting the Array to a Collection and using the .join() function. This method allows better control over the results. An array can be converted to a collection using the ARRAY TO COLLECTION command. The resulting collection can then be converted to a string using the .join() function which will accept a string as a parameter to act as a delimiter between the values. This will allow the values to be separated using a specific string such as a newline or tab.
ARRAY TO COLLECTION($varCollection; $textArray)

With the example code above, if $textArray contained the three elements {"a", "b", "c"} the results in $varText would be
"a   b   c"
with a tab in between each value, or

After converting the array to a string, the variable holding the string can easily be added to the expression pane in the debugger and its contents can be copied and pasted. The SET TEXT TO PASTEBOARD command or other methods can be used to export the results to a text editor to investigate the contents.