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

问题描述 投票:0回答:2

我有一个包含纯文本单元格和JSON数组的csv。我是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'}]

预期的返回值:

Pixar Animation Studios
TriStar Pictures, Teitler Film, Interscope Communications
Twentieth Century Fox Film Corporation
United States of America
json openrefine grel
2个回答
1
投票

如果你的数据看起来像这样...

enter image description here

...Tom Morris的公式就不能用了。似乎Open refine不喜欢Json中的单引号。另外,由于你有时会有几个 "名字",你必须用一个叫 "Json "的函数来检索它们。forEach() 循环。

公式如下。

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

意思是: 用""代替"",解析json,然后,对数组中的每个元素,把它放到一个变量v中,并得到它的值 "name"。最后,用逗号将得到的数组连接起来。

最后的结果。

enter image description here


0
投票

首先你需要把字符串解析成一个JSON对象 然后你就可以使用普通的基于键的字典访问来获取值了

value.parseJson()['name']

你可以使用这个表达式来添加一个新的列,或者使用Transform对现有的列进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.