span8
斯潘4
关于使用FME与JSON合作的介绍,请看教程:JSON入门.
在FME中,有许多方法可以操纵JSON来提取所需的数据。在本文中,we will cover three different transformers and highlight their uses,我是一个很好的朋友,the JSONExtractor,and the JSONFragmenter.在第一个例子中,we will extract weather information from a JSON API feed from MetaWeather the using the JSONFlattener and JSONExtractor.在第二个示例中,我们将获取在previous article并使用JSonFragCenter将其分割成包含多个记录的单个属性。
https://www.metaweather.com/api/location/9807/
我们将从元天气网站读取JSON消息,然后提取天气状态。Using the _response_body attribute created by the HTTPCaller we can flatten it using the JSONFlattener to expose the attributes.属性提取的另一种方法是选择要查询的特定JSON元素。要做到这一点,we will use the JSONExtractor extract the weather child element to see today's weather.
1。Add a Creator Transformer
打开FME工作台并启动一个空白工作区,add a Creator transformer to the canvas.
2.添加httpCaller转换器
单击Creator并添加一个httpCaller Transformer。The Creator will trigger the HTTPCaller to get information from the URL.In the HTTPCaller parameters,将请求URL的URL粘贴到下面,并将HTTP方法设置为获取:
https://www.metaweather.com/api/location/9807/
这个网址是温哥华的;if you would like to follow this example with your own city,您可以查找weid,然后替换URL末尾的数字字符串。
使用httpcaller从元天气API请求包含天气数据的URL
3.选择要保留的属性
我们只想保留httpcaller的响应主体属性,这是您在Web浏览器中打开URL时看到的JSON。Add an AttributeKeeper transformer to the HTTPCaller.在参数中,将要保留的属性设置为“响应”主体,然后单击“确定”。
4。Create attributes with the JSONFlattener
如果在attributekeeper之后运行并检查JSON,天气状态只有4个属性,没有单独的属性。温度,等。所以要访问这些,我们需要扁平化JSON。Add a JSONFlattener transformer and select _response_body for the JSON Document.现在,如果您运行并检查JSON,the _response_body has 139 attributes,包含我们所需要的所有信息,只需将它们分割成各个部分。
5。提取JSON以查找今天的天气
Instead of flattening the JSON into a set of attributes,if we are interested in one element or property we can extract that into an attribute.To find today's weather,我们需要提取父元素并公开子元素。Connect a JSONExtractor transformer to the HTTPCaller,and in the parameters Set the Source JSON Document to _response_body and then set the Target Attribute to Weather.对于JSON查询,输入json[“Consolidated_Weather”][0][“Weather_State_Name”]这将提取名为Consolidated_Weather的父元素,然后提取第一个数组(即0),然后打电话给那个叫天气州的孩子。这个API给了我们5天的预测,but we are only interested in today's weather,这就是为什么只调用第一个数组的原因。
Next,we need to extract the temperature,in the same JSONExtractor set another Target Attribute to Temperature and then set the JSON Query to json["consolidated_weather"][0]["the_temp"].If you wanted to call all 5 days of the forecast you can use [*] instead of [0].单击“确定”保存查询。
The syntax for the JSON Query,to query for weather in the JSONExtractor
It can be challenging to find the query to access the weather information.您可以仔细查看数据检查器中的属性,并注意这个结构:
Or an alternate way to find the query is by temporarily adding a FeatureReader to the canvas and reading in the JSON URL.Click on the parameters and set the Schema Scan Mode to ‘JSON Query'.然后在查询下,click the [...] to expose the JSON tree.In the tree expand JSON,然后扩大综合天气,and click on the child weather_state_name under.复制所选项目框中指示的JSON查询,这个查询与我们输入到JSonextractor中的查询相同。复制此查询后,可以删除FeatureReader。
Obtaining the correct JSON query syntax using the FeatureReader and the query tree
6。检查数据
将检查器连接到JSonextractor并运行转换。In the Data Inspector,您可以看到天气状态名称和温度属性已暴露。当您运行这个翻译时,天气的类型会有所不同,因为它是一个活动的JSON提要。
当前天气提取的JSON输出
在前面的示例中,我们读取JSON并使用JSonFlatener和JSonextractor检查输出。Just to review,jsonfrattener将扁平化json并公开响应主体内的属性。jsonextractor使用json查询提取属性,并在响应主体属性之外创建自己的属性。In this example,我们将在此基础上使用更嵌套的JSON,并提取纬度和经度来创建点。
1。使用文本文件阅读器读取JSON
在空白工作区中,or in another section of the previous workspace,add a Text File Reader.请阅读json编写概述文章中创建的vantraffic.json文件。在参数中,将“立即读取整个文件”设置为“是”。click ok.我们使用文本文件阅读器而不是JSON阅读器来读取JSON数据的原因是,我们希望JSON包含在一个名为text_line_data的属性中。如果我们使用JSON阅读器读取JSON,每一行都将是包含在其自身属性中的单独记录,包括支架。
2.添加一个JSonFragCenter,将JSON分解为单独的属性。
将JSonFragCenter连接到文本文件阅读器。In the parameters,set the JSON Attribute to text_line_data and set the JSON Query to json["features"][*],you can get this syntax by the methods stated in the previous example,通过浏览到json树并选择json>features>array values,以及复制语法。然后将扁平查询结果改为属性Yes,然后确保递归展平对象/数组设置为“是”。
Fragmenting the JSON using the JSONFragmenter and a JSON query
3.Expose attributes of interest
Add an AttributeExposer transformer,在参数中输入以下属性:
properties.Title |
属性.publishedDate |
properties.LinkURL |
属性。内容 |
属性ID |
FMEX几何{0 } |
类型 |
geometry.coordinates{} |
几何类型 |
我们可以在attributeexposer参数中输入父元素或子元素,including arrays as is the case with coordinate pairs.
Attributes to expose using the AttributeExposer exposing both parent and child elements
4。Create points with from the exposed attributes
Using the VertexCreator transformer,in the parameters set the X Value to the attribute geometry.coordinates{},在“列表元素选择”对话框中,输入0作为坐标。这将读取数组中的第一个坐标,这是经度值。Repeat this for the Y Value,选择1作为坐标读取数组中的第二个坐标,这是纬度值。
Extracting the coordinates out of the JSON array using an exposed attribute in the VertexCreator
5。将坐标投影到LL84中
Add a Reprojector transformer,and set the Destination Coordinates System to LL84 to reproject the points.
6。检查结果
Connect an inspector to the VertexCreator and run the translation.检查输出可以看到我们公开的每个属性都包含在每个点中。
提取顶点的输出并重新输入LL84
Data Attribution:
Example 1: The data used here originates from open data made available byMeta Weather
Example 2: The data used here originates from open data made available by the不列颠哥伦比亚省政府-BC路.
JSON读卡器配置
Converting from JSON to a spatial format (GIS)
JSON Reading Advanced
JSON Writing Overview
JSON Writing with JSONTemplater
?2019安全亚搏在线软件公司|Legal