Ask Your Question
1

Is there a way to remove a field from a record based on an evaluator?

asked 2018-04-05 08:46:11 -0500

mstang gravatar image

updated 2018-04-05 10:52:39 -0500

metadaddy gravatar image

I'm having some issues with NULL handling in a downstream JSON DB and I'm trying to remove a field from a record if it's value is null. I know I could create a new field when the value is not null as a workaround but I couldn't figure out if I could delete a field in a JS evaluator for example.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2018-04-05 11:21:41 -0500

metadaddy gravatar image

Yes - you can delete a field in a JavaScript evaluator. The trick is to realize that record.value is actually a Java LinkedHashMap and not really a JavaScript map. For example:

record = records[i];

if (record.value['a'] == null) {
  record.value.remove('a');    
}

There is one wrinkle - if you try to do that as you're iterating through the fields in a record, you'll get a ConcurrentModificationException, so you have to save the keys you want to remove, then remove them after the loop, like this:

record = records[i];

var keysToRemove = [];
for (var key in record.value) {
  if (record.value[key] == null) {
    keysToRemove.push(key);
  }
}

for (var j in keysToRemove) {
  record.value.remove(keysToRemove[j]);
}
edit flag offensive delete link more

Comments

Thanks! This is perfect, and it also answers a question I didn't ask which is "how do you iterate through the fields" :)

mstang gravatar imagemstang ( 2018-04-12 08:13:13 -0500 )edit
0

answered 2018-04-09 16:53:09 -0500

tmcgrath gravatar image

updated 2018-04-10 14:14:29 -0500

Another example outside of JS evaluator could be something similar to the following where a Stream Selector is configured to check for null condition and remove before writing to a destination.

In this example, stream 1 has been evaluated to true on the null check.

image description

edit flag offensive delete link more

Comments

Thanks, this would work if I was only concerned with one field, but I think it would be a bit harder to manage with many or an unknown group of fields.

mstang gravatar imagemstang ( 2018-04-12 08:12:02 -0500 )edit

Good point, I agree

tmcgrath gravatar imagetmcgrath ( 2018-04-12 08:20:02 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-04-05 08:46:11 -0500

Seen: 392 times

Last updated: Apr 10