Skip to main content
Answer

Hosted Extractors - added string functionality

  • November 23, 2023
  • 3 replies
  • 65 views

Forum|alt.badge.img

I’m getting results from several topics using the # function at the end of my topic path-tree.

e.g. ‘~/Installations/Location/Vessel/Seapath/#’
where ‘~/Installations/Location/Vessel/Seapath/acceleration_x’ is an example path

The problem now is that: when I get “context.topic” I get the full path (~/Installations/Location/Vessel/Seapath/acceleration_x) instead of the last path (acceleration_x) and I can’t seem to find a way to extract only the last path as there is barely any functionality in Functions | Cognite Documentation.

Is there a way to get past this?

sample data: {"timestamp":1700740159863,"values":[-0.013358482159674168]}
here is my transformation:
{
  "externalId": concat("installation.vessel.seapath.", context.topic_last_part), # where I can e.g. do split, substring, slice, regexp_extract or any string manipulation on context.topic
  "timestamp": input.timestamp,
  "value": input.values[0],
  "type": "datapoint"
}
sample output;

{
    "externalId": "installation.vessel.seapath.acceleration_x",
    "timestamp": 1700740159863,
    "type": "datapoint",
    "value": -0.013358482159674168
}

Best answer by mathialo

Hi! And thanks for the question.

Currently, there are no way of doing this, but we are currently in the process of adding several features for string manipulation, including both a split and substring function.

So very soon you will be able to do

"externalId": concat("installation.vessel.seapath.", context.topic.split("/")[5])

or

"externalId": concat("installation.vessel.seapath.", context.topic.substring(40))

 

3 replies

  • Practitioner
  • Answer
  • November 24, 2023

Hi! And thanks for the question.

Currently, there are no way of doing this, but we are currently in the process of adding several features for string manipulation, including both a split and substring function.

So very soon you will be able to do

"externalId": concat("installation.vessel.seapath.", context.topic.split("/")[5])

or

"externalId": concat("installation.vessel.seapath.", context.topic.substring(40))

 


  • Seasoned
  • November 30, 2023

We’re seeing similar cases, so +1 for this feature.


  • Practitioner
  • December 5, 2023

You can now use “substring”, “split”, “tail”, and “slice”, documentation for all these coming shortly.

 

In this case you could do

 

context.topic.split("/").tail(1)

or

context.topic.split("/").slice(-1)[0]