07-12-2018 07:56 AM
I am using a mapper snap to extract data from a json schema. This is the code from the expression
{id:$id}.extend($.fieldValues.map (elem => [elem.id, elem.type, elem.value]))
What I want to do is place this in a pipeline parameter (so can change this equation from the config file).
I can’t figure out the expression syntax. Here is a screenshot of my latest failure. 🙁
07-13-2018 12:39 PM
Have you tried putting it in an expression library and then reference it there?
For example, I have an expression library file invItems.expr:
{
INVOICE_TARGET_DATE: _invoice_date || Date.now().toLocaleDateString(‘{"timeZone":"EST", “format”:“yyyy-MM-01”}’)
}
and then i reference that in the pipeline:
lib.invItems.INVOICE_TARGET_DATE
07-13-2018 12:41 PM
As you found out later, the value of a pipeline parameters is a string. In order to evaluate it, you’ll need to use the ‘eval()’ function, like so:
eval(_field1)
Alternatively, you can put the expression in a file and import that file as a library (see Expression Library). Your library file would contain an object with a field that contains your function, like so:
{
extract: doc => {id:id}.extend(doc.fieldValues.map (elem => [elem.id, elem.type, elem.value]))
}
You can then execute that function in your mapper with an expression like this:
lib.mylib.extract($)
07-13-2018 01:19 PM
Awesome that worked!
I did not try the library but it is an interesting concept.
Personal note for anyone else struggling with this. I am would suggest using the library suggestion or a parameter for the entire function. I spent a ton of time trying to variablize out the function and this path is much easier.
Another point. You can’t use a variable with the json schemas in a mapper, it has to be a parameter.
Hope this helps! 😀