Convert Boolean to String

Hi guys,

Please does anyone have a solution to convert columns which have been detected as Boolean into the string data type?

I have multiple columns from multiple tables, so I need a solution that does not require the specification of a column name or the provisioning of dummy column headers. I have already been able to isolate the Boolean columns using the column splitter node, now I just need something to make the data type conversion.

Thanks!

Hey there,

this actually turned out to be a bit more tricky…

Initial thought was to use String Manipulation (Multi Column) with expression string($$CURRENTCOLUMN$$), however that fails…

Best alternative I found:

Use Table Manipulator Node to change the value type (select String => String) from drop down:

/Update: if any new columns are expected needs to default to String as well.

Demo WF here:

booltostring.knwf (84.6 KB)

Alternatively, depending on how you load the data, you could change it in the initial reader in the Transformation tab e.g. of an Excel Reader node.

4 Likes

Looks like I have found a cheat by coincidence, or shall we say a serendipitous blessing from the universe.

Step 1: Isolate the Boolean columns by setting the Boolean data type filter in the column splitter node.

Step 2: Run the columns through the number to string node

Step 3: Use the multi column string manipulation node to replace ‘0’ with ‘false’ and ‘1’ with true.

Step 4: Use the column appender node to join these processed columns to the rest of your data.

This solution should work with varying tables and column names since I am using data type filters and multi column nodes.

For the number to string node, ensure you include unknown column names, and enforce exclusion with an empty list for the multi string manipulation nodes.

3 Likes

Thanks for the response, Martin!

This works only if the setting for any unknown new column is set to string. If it is set to default, once I change the source table, the new Boolean columns still come through as Boolean.

Please update your solution accordingly so that I can mark it as the solution :slight_smile:

1 Like

Hi @TosinLitics , this is not a “core” solution to your question, which I feel you already have a solution for, but is instead a shameless plug for my Rule Engine (Multi Column) component :wink:

It has a slightly extended syntax over the standard Rule Engine, but works more or less the same way. As you can see below, it works across multiple columns using a $CURRENTCOLUMN$ similar to the way String Manipulation (Multi-Column) works, but also allows you to specify column type within the rule, by using "<CURRENTCOLUMNTYPE>" (rule needs to include the double-quotes as shown below)

The following rule would convert your booleans:

"<CURRENTCOLUMNTYPE>"="Boolean value" AND $CURRENTCOLUMN$ = "true" => "true"
"<CURRENTCOLUMNTYPE>"="Boolean value" AND $CURRENTCOLUMN$ = "false" => "false"
TRUE => $CURRENTCOLUMN$

I’d like to think that one day, KNIME may have a similar extended multi-column rule engine built in (it really should have one), and this gives idea on how it might work.

1 Like

Fair point - if the data structure changes over time that’s a neat solution. Updated my initial response.

Takbb’s component also pretty neat :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.