Question

How does one create a map from a set of fields?

  • 31 January 2022
  • 3 replies
  • 729 views

Userlevel 1

There is a function, str:splitKV which creates a map from a string, provided the key value pairs are encoded in the string, but how does one create a map from a group of individual fields?  I suppose one could convert the values into a single string with the necessary key/value pairings and then use the str:splitKV function, but such a conversion seems excessive and might require extra type conversions.

Is there a better way?


3 replies

Userlevel 3
Badge

hi @mblahay 

I’m not sure I fully understand what you’re asking, but let me try with this take first, if I misunderstood, please clarify the ask!

Data Collector provides a function to create a new Map field, it’s ${emptyMap()}. You can then use simple expressions to create new entries underneath it with values from your existing record, as you can see here:

The result is this; the Field Remover “Keep Map Only” can then be used to drop the unwanted fields from the record.

 

Userlevel 1

I am looking for something that I can use in a Field Replacer. Lets say I have an array that contains text. The text within each element is comprised of fixed width fields. I would like to convert this data from an array of text to an array of maps. Field Replacer seems to be the only stage that allows me to reference the array elements in place and replace the contents of the elements with the result of some operation. However, there doesn’t seem to be an operation I can perform on the text that is akin to NewMap(“field1”,substr(text,1,10),”field2”,substr(text,10,20),etc...) that will allow me to perform the conversion.

The emptyMap solution above certainly creates a map, but can it somehow be applied across the elements of an entire array?

I thought of pivoting the array into individual records, but there doesn’t seem to be a way to aggregate everything back together.

Userlevel 5
Badge

@Giuseppe Mura 

Reply