The following examples show how to use the Velocity transformation to convert XML data from one format to another and to convert and augment JSON data.
XML conversion example
You have product data in an XML file that you want to convert to XML of a different format.
The XML file that you want to convert, products.xml, contains data in the following format:
You want to convert the XML so that each product is in its own element, for example, <milk>...</milk>. You also want to append a timestamp in a comment inside the XML file.
To convert the file, first create a source file that contains the path to the XML file that you want to convert so that the mapping can read data from the XML file. Then, create the mapping.
Create a text file called "filepath.txt" that contains the following line:
C:\XMLSources\products.xml
After you create the source file, create the mapping. The following image shows the mapping:
Configure the transformations in the following ways:
Source transformation
Configure the Source transformation to read data from the file that contains the file path, and configure the source formatting options.
On the Source tab, select a connection that can access the source file, set the source type to Single Object, and select filepath.txt as the source object.
Click Formatting Options and then configure the following properties:
Property
Value
Flat File Type
Delimited
Text Qualifier
None
Field Labels
Auto-generate
First Data Row
1
Velocity transformation
Configure the Velocity transformation input and create the template.
On the Input Format tab, configure the following properties:
Property
Value
Input Field
Incoming string field from the Source transformation.
Input Type
File
Format Type
XML
Variable Name in Template
root
Code Page
UTF-8
On the Velocity Template tab, enter the following template in the template editor and validate the syntax:
<?xml version="1.0" encoding="UTF-8"?> <!--Generation date: $function.call('Systimestamp')--> ## Convert each product to an element: <products> #foreach ($child in $root.getRootElement().getChildren() ) <$child.getChild("name").getText()> <id>$child.getChild("id").getText()</id> <size>$child.getChild("size").getText()</size> </$child.getChild("name").getText()> #end </products>
Target transformation
Configure the Target transformation to write data to a flat file target created at run time. To ensure that the target file contains no header and no text qualifier character, configure the formatting options.
On the Target tab, select the connection and set the target type to Single Object. Click Select next to the Object field. Select Create New at Runtime, and enter the file name "products_converted.xml."
Click Formatting Options and set the text qualifier to None.
In the Advanced properties, set the header options to No Header.
When you run the mapping, the target file, products_converted.xml, contains the following XML:
You have vendor data from an online review service that is stored in a JSON BLOB in a database. You want to filter the records, convert the data to a different format, and write it to a JSON file. You also want to augment the data with additional attributes.
The database contains a JSON BLOB that contains data in the following format:
You want to filter the records so that they only include restaurants with three or more stars. You also want to add the city to each record based on the postal code.
The postal codes and corresponding cities exist in a flat file with the following format:
postal_code|city 15090|Wexford, PA 15102|Bethel Park, PA 15206|Pittsburgh, PA 15317|Canonsburg, PA 28012|Belmont, NC 28027|Concord, NC ...
You want the target file to contain JSON data in the following format:
To convert the data, create a mapping with a Velocity transformation that converts the JSON data and an unconnected Lookup transformation that returns the city based on the postal code.
The following image shows the mapping:
Configure the transformations in the following ways:
Source transformation
Configure the Source transformation to read data from the database table that contains the JSON BLOB.
Velocity transformation
Configure the Velocity transformation input and create the template.
On the Input Format tab, configure the following properties:
Property
Value
Input Field
Incoming string field from the Source transformation that contains the JSON BLOB.
Input Type
Buffer
Format Type
JSON
Variable Name in Template
inputRoot
Code Page
Default
On the Velocity Template tab, enter the following template in the template editor and validate the syntax:
Configure the Lookup transformation to return the city based on the postal code. The Lookup transformation must be an unconnected Lookup transformation so that you can call it from the template in the Velocity transformation.
On the General tab, select Unconnected Lookup.
On the Incoming Fields tab, create an incoming field for the postal code called in_postal_code.
On the Lookup Object tab, select the text file that contains the postal codes and cities. Then, click Formatting Options and configure the following properties:
Property
Value
Flat File Type
Delimited
Delimiter
Other: |
Text Qualifier
None
Field Labels
Import from Row 1
First Data Row
2
On the Lookup Condition tab, configure the following lookup condition:
postal_code = in_postal_code
On the Return Fields tab, select city as the return field.
Target transformation
Configure the Target transformation to write data to a flat file target created at run time. To ensure that the target file contains no header and no text qualifier character, configure the formatting options.
On the Target tab, select the connection and set the target type to Single Object. Click Select next to the Object field. Select Create New at Runtime, and enter the file name "vendors.json."
Click Formatting Options and set the text qualifier to None.
In the Advanced properties, set the header options to No Header.
When you run the mapping, the target file, vendors.json, contains the following data: