Help updating multiple Meta fields with a ternary?

#1

I’m having trouble updating the meta field with a ternary.

I want to be able to update the “meta.updated” field if i can provide the timestamp.

I’ve ran the dateTime by itself and it works:

 meta : ~ expression {updated: dateTime(source_timestamp, 'yyyy-MM-dd HH:mm:ss.SSSSSS') , sourceSystem: 'MySource1'}

I thought this would work for the transform once I add the ternary:
meta : ~ expression "source_timestamp ? ({updated: dateTime(source_timestamp, 'yyyy-MM-dd HH:mm:ss.SSSSSS') , sourceSystem: 'MySource1'}) : null"

An example for my ‘source_timestamp’ = “0001-02-03 04:05:08.700000”

For some reason this gives an error saying invalid format for ‘yyyy-MM-dd HH:mm:ss.SSSSSS’

I’ve tried
meta : ~ expression ‘{“updated”: “exists(source_timestamp) ? dateTime(source_timestamp, ‘yyyy-MM-dd HH:mm:ss.SSSSSS’) : null” , “sourceSystem”: “MySource1”}’
meta : ~ expression ‘{“updated”: “exists(source_timestamp) ? dateTime(source_timestamp, “yyyy-MM-dd HH:mm:ss.SSSSSS”) : null” , “sourceSystem”: “MySource1”}’
but getting errors saying missing “)” or “}”

Any ideas?

0 Likes

#2
0 Likes

#3

The error isn’t because of the dateTime transform. It’s due to the ternary

0 Likes

#4

Maybe try inverting the quotes, as follows:

meta : ~ expression ‘source_timestamp ? {updated: dateTime(source_timestamp, “yyyy-MM-dd HH:mm:ss.SSSSSS”) , sourceSystem: “MySource1”} : null’

I’ve seen funny behaviours depending on the quotes you use (there is a great article by @matt about quotes that you can find on the site: Quotes and escaping in transform expressions)

0 Likes

#5

Maybe try something like:

meta: ~ expression {
  updated: "exists(source_timestamp) ? dateTime(source_timestamp, 'yyyy-MM-dd HH:mm:ss.SSSSSS') : null",
  sourceSystem: "'MySource1'"
}
0 Likes

#6

What if source_timestamp is a number of seconds since epoch?
Apparently, using period(source_timestamp, 'SECOND') takes way too long.

0 Likes