Table of Contents

Search

  1. Preface
  2. Transformations
  3. Source transformation
  4. Target transformation
  5. Access Policy transformation
  6. B2B transformation
  7. Aggregator transformation
  8. Cleanse transformation
  9. Data Masking transformation
  10. Data Services transformation
  11. Deduplicate transformation
  12. Expression transformation
  13. Filter transformation
  14. Hierarchy Builder transformation
  15. Hierarchy Parser transformation
  16. Hierarchy Processor transformation
  17. Input transformation
  18. Java transformation
  19. Java transformation API reference
  20. Joiner transformation
  21. Labeler transformation
  22. Lookup transformation
  23. Machine Learning transformation
  24. Mapplet transformation
  25. Normalizer transformation
  26. Output transformation
  27. Parse transformation
  28. Python transformation
  29. Rank transformation
  30. Router transformation
  31. Rule Specification transformation
  32. Sequence Generator transformation
  33. Sorter transformation
  34. SQL transformation
  35. Structure Parser transformation
  36. Transaction Control transformation
  37. Union transformation
  38. Velocity transformation
  39. Verifier transformation
  40. Web Services transformation

Transformations

Transformations

JSON conversion example

JSON conversion example

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:
{"items":[ {"business_id":"1SWheh84yJXfytovILXOAQ","name":"Rancho Golf Club","address":"1200 E Camino Acequia Drive","postal_code":"85016","stars":3.0,"review_count":5,"is_open":0,"attributes":{"GoodForKids":"False"},"categories":"Golf, Active Life","hours":null}, {"business_id":"QXAEGFB4oINsVuTFxEYKFQ","name":"Garden Blessing Chinese Restaurant","address":"25 Eglinton Avenue W","postal_code":"L5R 3E7","stars":2.5,"review_count":128,"is_open":1,"attributes":{"RestaurantsReservations":"True","GoodForMeal":"{'dessert': False, 'latenight': False, 'lunch': True, 'dinner': True, 'brunch': False, 'breakfast': False}","BusinessParking":"{'garage': False, 'street': False, 'validated': False, 'lot': True, 'valet': False}","Caters":"True","NoiseLevel":"u'loud'","RestaurantsTableService":"True","RestaurantsTakeOut":"True","RestaurantsPriceRange2":"2","OutdoorSeating":"False","BikeParking":"False","Ambience":"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': True}","HasTV":"False","WiFi":"u'no'","GoodForKids":"True","Alcohol":"u'full_bar'","RestaurantsAttire":"u'casual'","RestaurantsGoodForGroups":"True","RestaurantsDelivery":"False"},"categories":"Specialty Food, Restaurants, Dim Sum, Imported Food, Food, Chinese, Ethnic Food, Seafood","hours":{"Monday":"9:0-0:0","Tuesday":"9:0-0:0","Wednesday":"9:0-0:0","Thursday":"9:0-0:0","Friday":"9:0-1:0","Saturday":"9:0-1:0","Sunday":"9:0-0:0"}}, {"business_id":"gnKjwL_1w79qoiV3IC_xQQ","name":"Fujiyama Japanese Cuisine","address":"111 Johnston Rd, Ste 15","postal_code":"28210","stars":4.0,"review_count":170,"is_open":1,"attributes":{"GoodForKids":"True","NoiseLevel":"u'average'","RestaurantsDelivery":"False","GoodForMeal":"{'dessert': False, 'latenight': False, 'lunch': True, 'dinner': True, 'brunch': False, 'breakfast': False}","Alcohol":"u'beer_and_wine'","Caters":"False","WiFi":"u'no'","RestaurantsTakeOut":"True","BusinessAcceptsCreditCards":"True","Ambience":"{'romantic': False, 'intimate': False, 'touristy': False, 'hipster': False, 'divey': False, 'classy': False, 'trendy': False, 'upscale': False, 'casual': True}","BusinessParking":"{'garage': False, 'street': False, 'validated': False, 'lot': True, 'valet': False}","RestaurantsTableService":"True","RestaurantsGoodForGroups":"True","OutdoorSeating":"False","HasTV":"True","BikeParking":"True","RestaurantsReservations":"True","RestaurantsPriceRange2":"2","RestaurantsAttire":"'casual'"},"categories":"Sushi Bars, Restaurants, Japanese","hours":{"Monday":"17:30-21:30","Wednesday":"17:30-21:30","Thursday":"17:30-21:30","Friday":"17:30-22:0","Saturday":"17:30-22:0","Sunday":"17:30-21:0"}} ...
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:
{ "Date": "<date>", "vendors":[ { "name": "<restaurant name>", "location": "<city, state/province>", "desc": "<description>", "stars": <number of stars> } , ...
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:
The mapping contains a Source transformation that is connected to a Velocity transformation that is connected to a Target transformation. The mapping also contains an unconnected Lookup transformation.
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:
#set($comma ="") { "Date": "$function.call('Systimestamp')", "vendors":[ #foreach($vend in $inputRoot.items) #if($vend.categories.toString().contains("Restaurants") && ($vend.stars > 3))$comma { "name": "$vend.name", "location": "$function.call(':lkp.lkp_CityLookup', $vend.postal_code)", "desc": "$vend.categories", "stars": $vend.stars } #set($comma =",") #end #end ] }
Unconnected Lookup transformation
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:
{ "Date": "07/08/2020 12:33:44.647037", "vendors":[ { "name": "Fujiyama Japanese Cuisine", "location": "Charlotte, NC", "desc": "Sushi Bars, Restaurants, Japanese", "stars": 4.0 } , { "name": "D'Amico's Pizzeria", "location": "Mentor-on-the-Lake, OH", "desc": "Italian, Restaurants, Pizza, Chicken Wings", "stars": 4.0 } , { "name": "Villa Borghese", "location": "Las Vegas, NV", "desc": "Restaurants, Italian", "stars": 4.0 } , { "name": "3-Rivers Diner", "location": "Pittsburgh, PA", "desc": "Sandwiches, Salad, Restaurants, Burgers, Comfort Food", "stars": 4.0 } , ...

0 COMMENTS

We’d like to hear from you!