什么 GREL 表达式用于从 OpenRefine 的单元格中获取 JSON 值?

What GREL expression is used to get JSON values from cell in OpenRefine?

我有一个 csv,其中包含单元格中的纯文本条目以及 JSON 数组。我是 OpenRefine 和 GREL 的新手,无法找到用于清理此 csv 的表达式。我只想获取 JSON 数组中 "name" 键的值。

示例单元格:

[{'name': 'Pixar Animation Studios', 'id': 3}]
[{'name': 'TriStar Pictures', 'id': 559}, {'name': 'Teitler Film', 'id': 2550}, {'name': 'Interscope Communications', 'id': 10201}]
[{'name': 'Twentieth Century Fox Film Corporation', 'id': 306}]
[{'iso_3166_1': 'US', 'name': 'United States of America'}]

预期 return 值:

Pixar Animation Studios
TriStar Pictures, Teitler Film, Interscope Communications
Twentieth Century Fox Film Corporation
United States of America

首先您需要将字符串解析为 JSON 对象,然后您就可以使用普通的基于键的字典访问来访问这些值。

value.parseJson()['name']

您可以使用此表达式添加新列或使用转换对现有列进行操作。

如果您的数据是这样的...

...Tom Morris 的公式行不通。 Open refine 似乎不喜欢 Json 中的单引号。此外,由于您有时有多个 "names",因此您必须使用 forEach() 循环来检索它们。

公式如下:

forEach(value.replace("'", '"').parseJson(), v, v.name).join(',')

意思是:将'替换为",解析json然后,对于数组中的每个元素,将其放入变量v中并获取其值"name"。最后,加入带逗号的结果数组。

最终结果: