span8
span4
本文介绍读取JSON数据。FME有几个方法来读取JSON数据到您的工作空间。每个人都有,他们是更有用的不同的方案。有些是JSON的规格,并拥有自己的读者格式,包括以GeoJSON,ESRI的JSON和IMDF,在这个表中总结:
方法 |
常见的情形 |
教程 |
JSON阅读器 |
从已知的文件路径或URL读JSON |
这一页 |
FeatureReader |
从翻译的中间有一个已知的或基于属性的文件路径或URL读JSON |
教程:FeatureReader变压器 |
JSON变压器(JSONExtractor,JSONFlattener和JSONFragmenter) |
阅读JSON直接进入从文件路径或URL,控制它是如何变成FME功能变压器 |
转化JSON使用JSONExtractor,JSONFlattener和JSONFragmenter |
HTTPCaller |
从API调用阅读JSON |
教程:入门的API |
GeoJSON的读者 |
源JSON数据遵循GeoJSON的规范 |
GeoJSON的读/写文件 |
ESRI JSON阅读器 |
源JSON数据如下ESRI的JSON规范 |
ESRI JSON的读/写文件 |
IMDF阅读器 |
源JSON数据如下IMDF以GeoJSON规范 |
IMDF读/写文件 |
本文介绍如何直接读成JSON使用JSON读者您的工作空间。它显示了两种不同的读者模式扫描模式:自动和JSON查询。对于其他的方案,请参阅上面的链接。
我们访问美国联邦航空局机场状态Web服务API在本教程中。您可以访问API documentation at SwaggerHub。
FAA-airport-status-example.json是在情况下,API是不可用的数据的样品的提取物。
Note:如果此API的URL改变了,你可以查看案例数据来代替。
1.View JSON data from a URL
首先我们来看看我们的源JSON数据开始。该示例数据包含有关美国机场的状态信息,包括属性,如目前的知名度和天气。我们要读(代码SFO)旧金山机场,这是可以从这个实时数据馈送:
https://soa.smext.faa.gov/asws/api/airport/status/SFO
如果你在浏览器中打开这个网址,你会看到你收到一个XML响应,因为这API默认提供XML。如果你想获得JSON,你有权要求它在HTTP标头。
2.View JSON data from an HTTP request with headers
我们可以做一个适当的HTTP请求头与请求JSON在浏览器中使用SwaggerHub,它是用于由FAA设计并记录他们的API的API平台。
前往API's getAirportStatus operation on SwaggerHub。然后:
Click the “Try it Out” button, which lets you provide parameters to the getAirportStatus operation.
Type “SFO” into the airportCode parameter field.
Click the “Execute” button, which will make an API request using the entered parameter.注意,对于“响应的内容类型”的默认值是“应用程序/ JSON”,这意味着此API请求将提供一个首部请求JSON作为响应。
在点击“执行”,你会看到一些JSON响应身下出现的HTTP 200代码:
这JSON是关于SFO的状态的数据,我们将使用演习。它应该是这个样子:
{ “名称”: “旧金山国际机场”, “城市”: “旧金山”, “国家”: “CA”, “国际民航组织”: “KSFO”, “IATA”: “证券及期货条例”, “SupportedAirport”:真实, "Delay": false, "DelayCount": 0, "Status": [ { "Reason": "No known delays for this airport" } ], "Weather": { "Weather": [ { "Temp": [ "Fair" ] } ], "Visibility": [ 10 ], "Meta": [ { "Credit": "NOAA's National Weather Service", "Url": "http://weather.gov/", "Updated": "Last Updated on Oct 9 2019, 6:56 am PDT" } ], "Temp": [ "53.0 F (11.7 C)" ], "Wind": [ "West at 11.5" ] } }
JSON Code example
1.Open FME Workbench and add a JSON Reader
点击阅读>添加读卡器。选择JSON(JavaScript对象符号)作为格式。
2.Select a Dataset to Read
你可以提供一个本地文件(例如在上面的下载部分)或数据集参数中的URL路径。
本文假设你正在使用的URL。糊https://soa.smext.faa.gov/asws/api/airport/status/SFO到DataSet参数。尽管我们知道这个URL以XML响应,FME will automatically include the correct headers to request JSON when using the JSON Reader.
Click the Parameters button.Setting JSON reader parameters let you control how FME turns the nested structure of JSON into features.There are two options for Schema Scan Mode: Auto and JSON Query.Auto mode simply takes the JSON parent attributes and turns those into FME attributes and turns the rest of the nested structure into FME list attributes.JSON Query mode, which we will examine later in the article, lets you write a JSON Query to pick which part of the JSON data structure you wish to use as the top level to define FME attributes.
For now, make sure that Schema Scan Mode is set to Auto:
Click OK.
Your Add a Reader dialog should look like this:
Click OK.
3。Run Workspace
Ensure that Feature Caching is enabled then click the Run button to run your workspace and create a local feature cache of the JSON data.
4.Inspect the JSON using Visual Preview
Click on your JSON reader feature type (or its green cache icon) to inspect it using the Table View of Visual Preview.You should see that the JSON data has been flattened into a table, with all of the top-level JSON attributes becoming FME attributes: Name, City, State, etc.
You will notice that no attributes with children are included, like “Status” and “Weather.” These attributes are still available on FME features, but because of the nested structure of JSON, they are read as lists.
Note: if you have not worked with unexposed attributes or lists in FME before, working with JSON might be challenging.You can readWorking with List Attributesfor a quick introduction to these topics.
5.View Features in the Feature Information Window
To find these unexposed attributes, click on the single feature in the Table View to select it.Then look in your Feature Information Window in Visual Preview.You can see all the attributes with children are stored on the feature as lists.They usedot notationwhere their children are attributes and FME list bracket notation where there is more than one attribute-value pair in the array:
In the next part of this article, you will learn how to change how the JSON structure becomes FME features.
1.Change JSON Reader Schema Scan Mode to JSON Query
Now let’s try reading in JSON using a JSON Query.
Expand your JSON Reader in the Navigator window, then Parameters, then double-click Schema Scan Mode:
2.创建 a JSON Query
To construct a JSONQuery:
设置模式扫描模式,以JSON查询。
在根据要素类表,输入“天气“。
然后点击查询下,并在[...]按钮的空间。这个按钮打开了JSON数据的嵌套结构,你可以用它来构建查询。
点击包含嵌套列表对象的箭头。从列表中选择天气;那么该查询将显示JSON [“天气”]。
单击确定:
3。Run Workspace
Run the workspace and then inspect the new feature cache.The Table View will report the data has no schema, but using the Feature Information Window reveals list attributes.Only the child data of weather and not the parent data such as Airport, City, etc., are included:
Previous Article:
Next Articles:
Converting from JSON to a spatial format (GIS)
JSON Reading Advanced
JSON Writing Overview
JSON Writing with JSONTemplater
Data Attribution
The data used in this article come from theFederal Aviation Administration’sAirport Status Web Service (ASWS) API, which is licensedCreative Commons 0 (CC0)。
©2020安全亚搏在线软件公司|法律