Error evaluating expression: Argument 1 of function "replace" does not match function signature

#1

Dear All,

While creating an JSONata expression I have encountered an error :

"Error evaluating expression:
Argument 1 of function "replace" does not match function signature"

but when I put .(dot) after the payload in the JSONata expression then an array of object is returned. However, I want the output to be a single object.
Can anyone please explain what's happening and how to correct this, if it's possible.

Note: I am new to JSONata and also I don't have deep knowledge of node-red either.

Link for Flow code:

https://drive.google.com/open?id=1-fnf7VnKE4rxh_rPiFpQ-BnzgfSQ2mUZ

Thanks
Hc

0 Likes

#2

Kudos for using JSONata to restructure your data! It's not the easiest syntax to grok, but it sure is nice for doing things like this...

If you want to just get an array of your sentences back in msg.payload, this expression works:

payload.(
   name & ' works for ' & $substringAfter(email, '@')
)

You can see how I tested it inside the change node's expression tester panel -- just copy your example input data in place of the payload's "hello world" value on the left side, then watch your output change as you type in your JSONata expression!

image

You stated that you want the results in one object, right? Does this do what you want?

{
   "result": payload.(name & ' works for ' & $substringAfter(email, '@'))
}

Of course, this just creates an array of strings as the "results" property of the payload -- so i'm not sure that extra level of structure is very helpful...

0 Likes

#3

Thanks for the reply. I really appreciate the time you have put to reply with much information. I am using the "result" so that I can further query on that accumulated data. The final output I wanted to have is a single string like:

(output at debug console) msg.payload : string[76]

"fred works for acmeA.inc, jack works for acmeB.inc, jill works for acmeC.inc"

@shrickus

0 Likes

#4

Sorry I missed this question... but if you are still looking for an answer, you can use the $join method on the array results. Just tack it onto the end of your expression, like this:

{
   "result": payload.(name & ' works for ' & $substringAfter(email, '@'))~>$join(", ")
}

The ~> operator calls the following function, passing its input as the first argument -- often called "tail recursion". It's a bit of syntactic sugar to make long expression easier to read than the traditional "nested functions", like this:

{
   "result": $join(payload.(name & ' works for ' & $substringAfter(email, '@')), ", ")
}
1 Like

#5

Hi Shrickus,

Thanks for your input. The job's done but I will surely try your method the next time I have to do something similar :slight_smile:

Thanks
Hc

0 Likes