ryan1234
ryan1234

Reputation: 7275

How to use ternary operator with Mule expression evaluators

In my Mule flow I want to use a ternary operation in a message enrichment element.

For example, here is the existing line that throws an error when the source is null:


        <enricher doc:name="Add revision number" target="#[variable:rev]" source="#[json:_rev]">

Here is the line when trying to use the ternary operation:


        <enricher doc:name="Add revision number" target="#[variable:rev]" source="#[json:_rev == null ? '' : json:_rev]">

If a null is returned from the enrichment source, I want to return an empty string instead. I don't want errors to be thrown when Mule tries to use the 'setProperty()' function to assign a null value.

Currently the ternary example works, but it returns 'false' (not as a string).

Upvotes: 2

Views: 6641

Answers (1)

David Dossot
David Dossot

Reputation: 33413

This is not the Mule Expression Language (MEL) but the old evaluator syntax, which doesn't support ternary expressions.

EDIT:

I suggest your get the MEL cheatsheet and see how JSON handling is done.

From the partial information you provide, I think you'll need a json-to-object-transformer in the response phase of your HTTP outbound endpoint (that fetches JSON right?). Then you can use a MEL ternary expression in your enricher.

Upvotes: 2

Related Questions