Feature Request - Function to Select Values by Key From Array of Dictionary

49 views
Skip to first unread message

Drew Chapin

unread,
Jul 31, 2018, 11:45:10 PM7/31/18
to automa...@googlegroups.com
Maybe there's already an easy way to do this and I just couldn't figure it out, but I would like a function that takes two values, an array of dictionaries, and a key name. The function would return a 1 dimensional array of each value of the given key name from each dictionary in the array.

For example:

=select([{"name":"Drew","age":30,"gender":"male"},{"name":"Michelle","age":31,"gender":"female"}],"name")

would return

["Drew","Michelle"]

using key name of age would give

[30,31]

Henrik "The Developer" Lindqvist

unread,
Aug 1, 2018, 2:49:27 PM8/1/18
to Automate
There's no built-in function for such specific use case. I'll likely implement lambda/function type so users can make custom filtering.
Until then use a For each, Expression true and Array add block to build result yourself.

Drew Chapin

unread,
Aug 4, 2018, 4:05:48 PM8/4/18
to automa...@googlegroups.com
I figured out a way to do this using jsonEncode() and findAll()...

Set Variable
data=[{"name":"Drew","age":30,"gender":"male"},{"name":"Michelle","age":31,"gender":"female"}]

Select names
=findAll(jsonEncode(data,"(?<=\"name\":\")[^\"]*(?=\")")

Outputs
["Drew","Michelle"]

Note: Makes the assumption that none of the name values contain a quotation mark.

Select ages
=findAll(jsonEncode(data,"(?<=\"age\":)[^,\\}]*(?=[,\\}])")
or
=findAll(jsonEncode(data,"(?<=\"age\":)\\d+")

Outputs
["30","31"]

Note: The returned items are of type text, not number.
Reply all
Reply to author
Forward
0 new messages