You are here: 网页 > jsontemplater.

jsontemplater.

使用FME功能属性值填充JSON文档。该文档作为模板提供,变压器将使用XQuery将属性值和几何信息插入模板。可以从属性,文件,或直接输入到变压器参数中的模板。

对于输入的每个功能Root输入端口,RootJSON template will be evaluated, and the result will be put into the attribute named by the Result Attribute parameter. The feature will then be output from the transformer.

JSON模板只是包含的JSON文档XQuery职能。基本上,这些功能是数据值的占位符。处理模板后,将评估功能,结果将插入文档中。大多数情况下,这用于将功能属性值插入到文档中。使用其他其他变换器中使用的相同XQuery函数指定要插入的属性的名称:XMLTemplater,XMLUpdater,XMLXQueryExtractor,XMLXQuery Exploder., etc.

在jsontemplater,fme:get-attribute,fme:get-list-attributeFME:get-json-属性功能特别有用。只允许不改变该功能的功能。看看XQueryFunctions documentation for more information on the available functions.

The following JSON template populates a JSON element with the value of the ‘id’ feature attribute.

{

“ID”:FME:Get-Attribute(“ID”)

}

If the attribute does not exist on the feature, the JSON key value will have a null value.

To populate a JSON array with the contents of a list attribute, thefme:get-list-attribute可以使用,如在此样本中:

{

"ids" : [ fme:get-list-attribute("ids{}") ]

}

If the function call is not contained in the square array braces, an array will automatically be created if the function returns more than one value. An array will not be created if the function returns one or zero values.

Sub-Templates

When a template is evaluated, it can only access the attributes of the feature which is currently being processed. In order to allow a template to access the attributes of multiple features, the concept of sub-templates was introduced. Using theFME:流程功能函数,变压器可以在一组功能上评估子模板,然后将结果插入第一个模板。在变压器接口中,每个子模板将被给出一个名称。然后将用于此名称FME:流程功能call to identify the sub-template to evaluate. In addition, a transformer input port will be created for each sub-template. The sub-template will be processed on features which enter the corresponding transformer input. Features which enter one of these input ports will be referred to as sub-features.

In the following example, theRoottemplate constructs a JSON object representing a village, with information from a single feature. It then populates the “houses” value, using attribute values from features which entered the HOUSE input port.

{

“姓名”:fme:get-attribute(“varion_name”),

“填充”:FME:Get-Attribute(“人口”),

“房屋”:[

FME:流程功能("HOUSE")

]
}

与House Input Port关联的模板创建一个House对象。

{

“地址”:FME:Get-Attribute(“地址”),

"owner" : fme:get-attribute("owner"),

“num_floors”:fme:get-attribute(“num_floors”)

}

如果单个功能进入Root输入和两个功能输入房屋输入,结果JSON文档可能如下所示:

{

“姓名”:“Anytown,USA”,

“人口”:2568,

“房屋”:[

{

“地址”:“123主街”,

“所有者”:“John Doe”,

"num_floors" : 2

},

{

“地址”:“324主街”,

“所有者”:“Jane Doe”,

"num_floors" : 3

}

]

}

模板可以调用许多子模板。例如,在上面的示例中,我们可以添加另一个名为Business的子模板,然后使用进程 - 功能函数来运行此子模板Root模板。另外,子模板可以运行另一个子模板,其中限制子模板可能不直接或间接运行自身。

选择要处理的子功能

Often there is a hierarchical structure to the features that enter the JSONTemplater. For example, each village will have a number of houses, and each house will have several rooms, etc. If theFME:流程功能使用如上所述的功能,所得到的文档可能不正确。假设有两个村庄,每个村庄都有五个房子。每个村庄和房屋都是由一个单独的FME功能表示。村庄的功能被路由到了Root输入端口,而房屋功能被路由到房屋输入端口。

If the transformer is run with the above templates, there will be two output features, as expected. However, both of the village objects will include all ten house objects. The correct behavior is to only evaluate the sub-template on the HOUSE features which correspond to the current village feature.

TheFME:流程功能函数提供了一种方法来做到这一点。额外的乐趣ction parameters may be used to filter the sub-features which are to be processed. The second parameter is a list of attribute names, and the third is a list of attribute values. Only sub-features whose attributes match the given list of attribute names and values will be processed.

This function call will evaluate the HOUSE sub-template on all HOUSE features whosevillage_idattribute matches the id attribute of the current feature.

FME:流程 - 功能(“House”,“Village_ID”,FME:Get-Attribute(“ID”))

More than one attribute/value pair can be specified. In this case, the attribute names and attribute values have to be contained in parenthesis. The following function call will evaluate the HOUSE sub-template on all HOUSE features whosevillage_idattribute matches the id attribute of the current feature, and whosenum_floors.属性是2。

FME:流程功能("HOUSE", ("village_id","num_floors"), (fme:get-attribute("id"), 2) )

Grouping Sub-Features

The Group Sub-Features By parameter allows for more coarse-grained filtering of sub-features than the parameters in theFME:流程功能function. When this parameter is set to a list of attribute names, theFME:流程功能function will only process sub-features which have the same values for these attributes as the feature currently being processed. Note that this parameter has no effect on theRootfeatures. EachRoot将从变压器处理和输出功能。

分组子功能时,如果每个组中的所有功能都是连续的,则可以通过将分组的子特征进行连续参数来提高变压器的性能,以便是。设置此值时,Root一旦所有适用的子特征到达变压器,就会处理功能。这意味着变压器将以非阻塞方式操作,而不是在开始处理模板之前等待所有功能。如果组中的功能并非全部连续到达,则变压器的行为是未定义的。如果发生这种情况,可能会不会处理一些子功能。

Running Multiple Sub-Templates with a Single Function Call

可以使用单个函数调用来评估多个子模板。只需将模板名称列表传递给FME:流程功能function. The sub-templates will be evaluated on each of the sub-features named in the list. The features will be processed in the order that they entered the transformer. The following function call processes the HOUSE and BUSINESS sub-templates:

FME:流程功能( ("HOUSE","BUSINESS") )

The sub-feature selection parameters may still be used when theFME:流程功能function is used to evaluate multiple sub-templates.

在同一功能上运行子模板

It is possible to evaluate a sub-template using the same feature which is being used to evaluate the current template. Thefme:process-templatefunction takes a name, or list of names, of sub-templates which should be evaluated. These templates will be evaluated and the results will be inserted into the current template. To evaluate a template, just enter the name of the sub-template as a function parameter. For example, while a HOUSE feature is being processed, we could evaluate the OWNER template using the following function call. The OWNER template will be evaluated using the feature which entered the HOUSE input port.

FME:流程模板(“所有者”)

要评估一组模板,请传递名称列表:

FME:流程模板((“所有者”,“地址”))

This technique may be used to modularize JSON templates, by moving repeated template structures into a single place. For example, if both houses and businesses have an address, the address could be extracted into an ADDRESS template, and the HOUSE and BUSINESS templates could then use thefme:process-template函数插入地址值。这样,地址模板不必在房屋和业务模板内重复。

几何模板

Geometry templates can be used to write out custom JSON geometry. There are a large number of functions which allow the extraction of geometric data, and the processing of sub-templates on geometries. For more information, see the XQuery Functions documentation for a list of all the geometry functions, and how to write out geometric data using sub-templates.

Geometry sub-templates operate in the same way as regular sub-templates, with the exception that a geometry sub-template does not create an input port on the transformer.

虽然正在评估几何模板,但是访问功能属性的函数(fme:get-attribute等),仍然可用。

Validation of Attribute and Sub-Template Names

通过模板表达式参数指定JSON模板或模板文件参数时,变压器将验证传入功能中是否存在所有引用的特征属性。如果从输入功能丢失(未公开)属性,则变压器将被突出显示为不完整的红色。发生这种情况时,变换器的摘要注释将指示JSON模板引用的丢失属性。

In addition, when sub-template names are passed to theFME:流程功能fme:process-template函数,将验证名称以确保它们与变压器接口中给出的名称匹配。

通过将参数验证属性/模板名称设置为No.,可以覆盖此附加验证行为

编辑变换器参数

使用一组菜单选项,可以通过引用工作区中的其他元素来分配变压器参数。一些变压器也可提供更高级的功能,例如高级编辑器和算术编辑器。要访问这些选项的菜单,请单击除适用的参数旁边。有关更多信息,请参阅变换器参数菜单选项.

变压器类别

Format Specific

网页

搜索FME知识中心

搜索有关此变压器的样本和信息FME Knowledge Center.