在Google Refine中解析JSON

时间:2012-04-24 18:54:00

标签: json parsing google-refine

我正在尝试使用Google Refine从Data Science Toolkit coordinate2politics API中提取特定元素。

以下是样本单元格#1:

[{"politics":[
 {"type":"admin2","friendly_type":"country","code":"usa","name":"United States"},
 {"type":"admin6","friendly_type":"county","code":"55_025","name":"Dane"},
 {"type":"constituency","friendly_type":"constituency","code":"55_02","name":"Second district, WI"},
 {"type":"admin5","friendly_type":"city","code":"55_48000","name":"Madison"},
 {"type":"admin5","friendly_type":"city","code":"55_53675","name":"Monona"},
 {"type":"admin4","friendly_type":"state","code":"us55","name":"Wisconsin"},
 {"type":"neighborhood","friendly_type":"neighborhood","code":"Eastmorland|Madison|WI","name":"Eastmorland"}
 ],"location":{"longitude":"-89.3259404","latitude":"43.0859191"}}]

我使用这个GREL语法添加了一个基于此专栏的专栏来推出该县,Dane:

value.parseJson()[0]["politics"][1]["name"]

但是当我进入Sample Cell#2时,语法不再有效,因为JSON结果有点不同:

[{"politics":[
 {"type":"admin2","friendly_type":"country","code":"usa","name":"United States"},
 {"type":"constituency","friendly_type":"constituency","code":"55_05","name":"Fifth district, WI"},
 {"type":"admin4","friendly_type":"state","code":"us55","name":"Wisconsin"},
 {"type":"admin6","friendly_type":"county","code":"55_079","name":"Milwaukee"},
 {"type":"admin5","friendly_type":"city","code":"55_84675","name":"Wauwatosa"},
 {"type":"constituency","friendly_type":"constituency","code":"55_04","name":"Fourth district, WI"}
 ],"location":{"longitude":"-88.0075875","latitude":"43.0494572"}}]

是否有某种方法可以对JSON进行排序或对我的语法进行短语,以便我可以在任何一种情况下找到该县?

更新

这是神奇的GREL,它允许我按名称在JSON字符串中查找元素,而不仅仅是位置:

filter(value.parseJson()[0]["politics"], item, item["type"]=="admin6")[0]["name"]

1 个答案:

答案 0 :(得分:6)

名为politics的字段是一个数组,您将返回:

value.parseJson()[0]["politics"]

该数组的一个元素与县相关联(它是friendly_type字段为“县”的那个)。因此,您需要过滤politics字段以找到friendly_type为县的字段,如下所示:

filter(value.parseJson()[0]["politics"], item, item["friendly_type"]=="county")

返回一个包含一个元素的数组。您希望从该一个元素中获取name字段,因此您需要提取第0个数组元素的name,从而生成完整的表达式:

filter(value.parseJson()[0]["politics"], item, item["friendly_type"]=="county")[0]["name"]