Set fallback values with JMESPath

I have merged two datasets, and prepare to make an update payload.

I want to create a fallback routine for the values so that if a value from set A does not exist, use the one from B. And if none of them exist then don’t include it.

I was hoping to achieve that by JMESPath but it seems Alumio does not tolerate setting the value to NULL (but I can set it to the string ‘NULL’).

Here’s what I did, simplified test input:

{
  "a": null,
  "b": "B"
}

Used a Value setter to set “c” to “&{a || b || `NULL`}”

Which gives us: “c”: “B”

But when both are null:

{
  "a": null,
  "b": null
}

we get "c": "NULL" (hoped to get "c": NULL)

Now I can use the Conditional transformer and check for string equals ‘NULL’ and filter that key out afterwards. But when there are several values we will have to filter each one.

Do you have some smarter/alternative ways of doing this?

Here’s a test transformer I made:
export_20230607170617.ndjson (2.3 KB)

Hi Kjetil,

Unfortunately, we don’t support returning null values as an output of a JMESPath syntax at the moment. But we can provide empty string or (''), and empty array (`[]`) as alternatives. Would those alternatives work in your situation?

Additionally, you can still map it as null with the help of the “Standard” mapper, such as below.

1 Like

Thanks, that’s very good that I can achieve to set the NULL value with such a mapper - better than doing a separate conditional check on null with a setter in this case!

I was thinking of just putting a bunch of fields into an object in one single value-setter, and set all the values with JMESPath in one go. Say we have 10 or 20 fields we want to map like this, then the one object with JMESPath fields would save us 9 or 19 setter-mappers. :grinning:

However I would not have guessed that I could use this mapper since an empty string is actually not the same as NULL? I think small things like this is important to know about in Alumio.

Yes, I can make sure that it’s shorter than doing conditional :slight_smile:

If you’re going to map all the fields inside an object with the same behavior, you can use Value Mapper instead and put the mapper there. For example, I create an object using a key called test by using a Value Setter and set the value to the below JSON object.

{
  "foo": "&{a || ''}",
  "bar": "&{a || ''}",
  "baz": "&{a || ''}"
}

In order to apply the mapper to all properties under the test, you can use a Value Mapper and put the same mapper there. We use “Pattern accessor” here as the accessor with pattern test.*, such as below.

Regarding your last question, it depends on the comparator selected. The “Adaptive equals comparison” means == in the PHP, where 0 == false == '' == null == []. We also have “Equals” comparator that means === in the PHP, where the compared values should have the same value and type.

In the future, hopefully we can create more documentation that explain more features in Alumio, including comparators.