cancel
Showing results for 
Search instead for 
Did you mean: 

How can we combine two fields without explicitly referencing them?

kiko
New Contributor

Hello,

we are trying to combine pairs of fields, without knowing their names in advanced.

INPUT
{ "field1": "val1", "field2": "val2" }

OUTPUT
{ "field12": "val1_val2" }

The output could be something different. Our goal is to check the value of both fields and depending on them generate a specific output. Is there any way to do something like that without having to refer to the field name?

Thanks

1 ACCEPTED SOLUTION

alchemiz
Contributor III

Hi Kiko,

I did use the _ from the key name to filter it out…

here’s the expression:

$input.mapValues((v,k)=> !(k).contains('_') ? $input.filter((i,j)=> j.startsWith(k)).values().join('_') : null).filter((v,k)=> !(v instanceof Null))

image

View solution in original post

3 REPLIES 3

cjhoward18
Employee
Employee

in this case listed above does the new key need to be generated from the old keys as well?

For the case above with your example input doc being in the variable $input here is an expression, when used in a Mapper Snap, that replicates the output:
$input.values().join('_') mapped to target path ‘field12’ will do what you explain.

If the key needs to be generated as well it will take a bit more logic in the expression.

Hello, many thanks for your input. The challenge I am facing is that I can’t directly reference the target path. I will have input pairs of fields that I will need to merge and all they have in common will be the starting part of the key. Let me provide another example:

INPUT
{ "name": "val1", "name_b": "val2", "surname": "val3", "surname_b": "val4" }

OUTPUT
{ "name": "val1_val2", "surname": "val3_val4"}

I am flexible to generate a new key or updating an existing one, but I can’t refer to them in the target path because in every execution the input fields will be different.

Thanks again

alchemiz
Contributor III

Hi Kiko,

I did use the _ from the key name to filter it out…

here’s the expression:

$input.mapValues((v,k)=> !(k).contains('_') ? $input.filter((i,j)=> j.startsWith(k)).values().join('_') : null).filter((v,k)=> !(v instanceof Null))

image