Hi,
Can anyone help me with converting multiple map into single map,
example input: {a!map(),a!Map(),a!map()}
output{a!map()}
Help is appreciated
Discussion posts and replies are publicly visible
What would this "conversion" look like? Just take the first one, or the last, or combine things somehow?
I have an output something like below, i would want to convert these 6 map items to single map and these field names are not static , field names can differ and are dynamic
So each map has an individual key and you want to combine these into a single map containing all key-value pairs?
salmak2626 Map function works with key value pair.What's key here?
Yes
it has key value pair and key are field names and value has associated value to it , i just need to merge into single map
a!localVariables( local!maps: { a!map(alpha: "a"), a!map(beta: 2), a!map(gamma: "three") }, local!keys: a!flatten( a!forEach( items: local!maps, expression: a!keys(fv!item) ) ), a!update( a!map(), local!keys, a!forEach( items: local!maps, expression: index( fv!item, a!keys(fv!item), null ) ) ) )
there are map inside map , because of that key will not fetch the key name , if i flatten complete array sets ,key wont be unique
example maps are attached and the error which i am getting is also been attached
I can't see how your data structure looks like, but I think you should be able to adapt my code to your needs. If you have a list of list of maps, it might be a good idea to put my code into a separate expression. Then use a foreach on the top level to iterate on each of the rows from the screenshot.
i will try to use it ,thanks for the help!
The above suggested code will give you the solution. You can use 'a!flatten()' to get a single list of map if you have map inside maps.
this would create an issue of duplicate key names
How can you create a single map with duplicate keys?
If you wish to modify the key names at run time then you can add additional logic after getting local!keys.
I have added an additional local!modifiedKeys in Stefan's code
a!localVariables( local!maps: { a!map(alpha: "a"), a!map(beta: 2), a!map(beta: 3), a!map(gamma: "three"), a!map(gamma: "four"), a!map(gamma: "five") }, local!keys: a!flatten( a!forEach( items: local!maps, expression: a!keys(fv!item) ) ), local!modifiedKeys: a!forEach( items: local!keys, expression: a!localVariables( local!indexCount: count( wherecontains( tostring(fv!item), touniformstring(local!keys) ) ), if( local!indexCount = 1, fv!item, concat(fv!item, fv!index) ) ) ), a!update( a!map(), local!modifiedKeys, a!forEach( items: local!maps, expression: index(fv!item, a!keys(fv!item), null) ) ) )
Thanks for the solution, I was able to use Stefan’s code and resolved my issue yesterday