Forum Discussion
The CSVParser has some functionality for doing type conversion, but the types are expected to be in a separate file (see the Input Views section of the doc).
If you are not able to get your data in that form, I’m attaching an example pipeline that might do what you want. This pipeline uses a Router snap to split the first row off and then a Join to merge it back in with all the remaining rows. A Mapper snap is then used to do the type conversion with the following expression:
$.mapValues(
(value, key) => match $types.get(key) {
'char' => value,
'integer' => parseInt(value),
'date' => Date.parse(value, "dd/mm/YY"),
_ => value
}
)
Since that’s a little involved, I’ll go into some more detail. First, the mapValues()
method is used to rewrite the value of each property in the input document. That method takes a callback that does the actual work. The callback uses the match
operator to check the type of each property and then executes the conversion expression (e.g. the type of “Priority” is “integer”, so the match arm with parseInt(value)
is executed).
TypeConversion_2019_09_16.slp (10.5 KB)
Thank you @tstack . This worked perfectly.
- stephenknilans8 years agoContributor
You can use a mapper with the nullsafe option turned on, and specify the fields that you need on the source and target. That should give you a null value. You can also simply replace the source value with a string like:
((‘LAST_NAME’ in $)?$LAST_NAME: null)
This is basically two commands:
- (X in Y) which returns true if X is in Y. $ means the variables in this path.
- (X ? Y: Z) which means that if X is true then Y else Z
Related Content
- 4 months ago
- 11 months ago