{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":934,"path":"api-reference/server-api-reference/TransformerDefinitionAPI.md","title":"TransformerDefinition - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# TransformerDefinition - Scoped, Global\n\nThe TransformerDefinition API provides methods to instantiate and manage the TransformerDefinition object, which associates a transformation rules list and a record path.\n\nThis API works along with the Transformer and TransformerRuleList APIs. Together, these APIs transform XML nodes or any entity in a structured JSON document into an output of name-value pairs. Supported JSON entities including objects and elements within an array, such as strings, numbers, and other arrays.\n\n-   The TransformerRuleList API enables you to create transformation rule lists that define what data in the source document to include in the output and how to transform the source data.\n\n-   The TransformerDefinition API associates a transformation rule list with a JSON/XML record path to define reusable transform definition objects. You can use a transform definition object to transform one or more source documents.\n\n\nIn addition, this object provides methods that enable you to obtain the record path of the TransformerDefinition object. It can also validate whether the definition has a valid TransformerRuleList associated with it.\n\nThe Transformer API performs the actual data transformation, one data entity at a time, using the specified transformation rule list to create the desired output data.\n\nYou can use the TransformerDefinition class in both scoped and global server scripts. When using this class, use the `sn_tfrm` namespace identifier. Also, before this API is available in an instance, you must activate the Transformation Service plugin \\(com.glide.transform\\).\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## TransformerDefinition - TransformerDefinition\\(Object transformerRuleList, String recordPath\\)\n\nInstantiates a TransformerDefinition object.\n\n<table id=\"table_sf4_rby_qhb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ntransformerRuleList\n\n</td><td>\n\nObject\n\n</td><td>\n\nTransformerRuleList to associate with this transformer definition.\n\n</td></tr><tr><td>\n\nrecordPath\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. The path \\(JSONPath for JSON, XPath for XML\\) that identifies the container \\(parent\\) of nodes to parse as records in the source document.\n\n For JSON documents, these nodes can be any object, array, or JSON primitive \\(string, number, boolean\\). For XML documents, these nodes are the child elements of the container.\n\n If **recordPath** is unspecified, the entire document is inferred as a single record. For JSON documents, if the top node is an array and **recordPath** is unspecified, then the top array is the record's container, and each of its entries is a record.\n\n</td></tr></tbody>\n</table>```\nvar stockAPI = new sn_ws.RESTMessageV2('Stock Details', 'Default GET'); \nvar response = stockAPI.execute(); \nvar responseBody = response.getBody(); \n\nvar transformerRuleList = new sn_tfrm.TransformerRuleList()\n  .fromJSON()\n  .addSymbol('ticker', 'quote.symbol') \n  .addSymbol('change_percentage', 'quote.change_change') \n  .thenMultiply('100') \n  .thenRoundDown('0') \n  .addSymbol('close_price', 'quote.close') \n  .thenAdaptCurrency('USD', false) \n  .addSymbol('summary') \n  .thenConcat('Shares of ') \n  .thenConcatSymbol('ticker') \n  .thenConcat(' closed at ') \n  .thenConcatSymbol('close'); \n\n \nvar path = '$.*'; // Transform all objects in the JSON document\nvar transformerDefinition = new sn_tfrm.TransformerDefinition(transformerRuleList, recordPath); \nvar transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody); \n\nvar results = []; \nwhile (transformer.transform()) { \n  results.push(transformer.getRow());\n}\n```\n\n## TransformerDefinition - TransformerDefinition\\(String transformerDefinitionSysId\\)\n\nInstantiates a TransformerDefinition object using an existing transformer definition.\n\n<table id=\"table_cfj_yr5_qxb\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ntransformerDefinitionSysId\n\n</td><td>\n\nString\n\n</td><td>\n\nSys\\_id of a transformer definition. Table: Record Transformer \\[sys\\_record\\_transformer\\]\n\n</td></tr></tbody>\n</table>This example instantiates a TransformerDefinition object based on the provided sys\\_id.\n\n```\nvar transformerDefinition = new sn_tfrm.TransformerDefinition(\"7208b59743542110d4dc2c78c9b8f2e2\");\n```\n\n## TransformerDefinition - recordPath\\(\\)\n\nIf set when the TransformerDefinition object was instantiated, returns the record path for the source data file associated with the TransformerDefinition object.\n\nA record path \\(JSONPath for JSON, XPath for XML\\) identifies the container \\(parent\\) of nodes to parse as records in the source document.\n\nFor JSON documents, these nodes can be any object, array, or JSON primitive \\(string, number, boolean\\). For XML documents, these nodes are the child elements of the container.\n\nIf **recordPath** is unspecified, the entire document is inferred as a single record. For JSON documents, if the top node is an array and **recordPath** is unspecified, then the top array is the record's container, and each of its entries is a record.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|String|If set, the record path associated with the transformer definition. If not set, returns null.|\n\n```\nvar path = '$.records'; \nvar transformerDefinition = new sn_tfrm.TransformerDefinition(transformerRuleList, recordPath); \nvar transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody);  \n.\n.\n.\n\nvar recordPath = transformerDefinition.recordfPath();\n\nvar results = []; \nwhile (transformer.transform()) { \n  results.push(transformer.getRow());\n}\n```\n\n## TransformerDefinition - validate\\(\\)\n\nValidates whether a valid TransformerRuleList is associated with the TransformerDefintion object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_klv_hgy_qhb\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether a valid TransformerRuleList is associated with the specified TransformerDefintion object.Valid values:\n\n -   true: Valid TransformerRuleList associated\n-   false: Invalid or no TransformerRulList associated\n\n</td></tr></tbody>\n</table>```\nvar transformerDefinition = new sn_tfrm.TransformerDefinition(transformerRuleList, recordPath);\nvar transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody); \n.\n.\n.\n\nif (transformerDefinition.validate()) {\n\n  var results = []; \n  while (transformer.transform()) { \n    results.push(transformer.getRow());\n  }\n}\n```\n\n"}}