Forum Discussion

jhagist's avatar
jhagist
New Contributor III
8 years ago

Problem with Pipeline Parameter in Mapper Snap

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. 🙁

7 Replies

  • tstack's avatar
    tstack
    Former Employee

    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($)
    
  • jhagist's avatar
    jhagist
    New Contributor III

    Thanks for this. I am currently using the pipeline parameter to pass this information.

    My question is really how to write the syntax.

    I have put the function (that I need to variablize) into the Pipeline parameter _field1 but I can’t get it to run

    • brianrandolph's avatar
      brianrandolph
      New Contributor III

      you shouldn’t need parenthesis around the name. “_field1” should work.

      • jhagist's avatar
        jhagist
        New Contributor III

        When I try it without the parenthesis around the name. “_field1” All I get is the parameter as a string.

        Snap,

        Any ideas??

  • jhagist's avatar
    jhagist
    New Contributor III

    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! 😀