Question

Dynamically create attributes using two lists: one with labels and one with values

  • 6 February 2019
  • 5 replies
  • 4 views

Badge

Hi,

For each feature in my dataset, I have two lists: one containing labels and one containing values.

label{0}: ID
label{1}: Name
label{2}: Type
label{3}: Country

val{0}: 1
val{1}: Groningen
val{2}: Place
val{3}: The Netherlands

There may be more or less items in both lists. The contents of `label` may be completely different. But `label` and `val` always have an equal number of items.

How can I dynamically create attributes from these lists, with the contents of `label` as attribute names and `val` as attribute values?

 


5 replies

Userlevel 2
Badge +12

Hi Willy,

 

Have a look at this FME Hub transformer:

This one worked for me.

Erik Jan

Badge

Hi Willy,

 

Have a look at this FME Hub transformer:

This one worked for me.

Erik Jan

Thank you @erik_jan. That looks like a good solution!

Badge

Update:

I added a PythonCaller with code based upon this example by @takashi

http://fme-memorandum-takashi.blogspot.com/2013/09/extract-list-attribute-elements-in.html

This does the job.

I'm also going to take a look at the solution @erik_jan provided. I 'd prefer a solution without Python code.

Userlevel 2
Badge +12

Hi Willy,

 

Have a look at this FME Hub transformer:

This one worked for me.

Erik Jan

Made a small example workspace: listkeypair.fmw

Userlevel 2
Badge +17

The ListKeyValuePairExtractor is a good solution.

Interestingly, JSON operations can do that too. FYI.

JSON Template Expression:

{|
    let $labels := fme:get-list-attribute('label{}')
    let $vals := fme:get-list-attribute('val{}')
    for $i in (1 to count($labels))
    return {$labels[$i] : $vals[$i]}
|}

0684Q00000ArKElQAN.png

Reply