The Transformation template
A transformation template transforms the results from a GraphQL query into a format compatible with CSV or Excel output.
In Custom Reporting, there are three output formats: JSON, CSV or Excel. A transformation template is required for CSV or Excel output, and not required for JSON output.
Some GraphQL query results fields can be in the form of objects or arrays of objects. Fields that are not strings or numbers must be flattened into simple string or numeric fields in order to be compatible with CSV or Excel output.
A transformation template uses
JMESPath, a query language for JSON, to accomplish this transformation.
Beyond making data compatible with CSV or Excel output, transformation templates can provide very powerful parsing and reshaping of custom report data. Useful when preparing data for complex Excel spreadsheets.
Minimal Transformation template example:
Where the query results look like this:
{
"data": {
"costLineItems": [
{
"assetType": "Amazon Elastic Compute Cloud",
"category": "Compute",
"ccCustomerTotalCostConverted": 47.205000461,
"providerName": "aws",
"quantity": 9442,
"serviceStartDate": "2020-11-01T00:00:00.000Z"
},
{
"more": "costLineItems..."
}
]
}
}
A minimal transformation template looks like this:
data.{MyReportName: costLineItems}
Where
data
is the primary node in the query results. Where
MyReportName
is a name you choose, and will become the name of the CSV file or the label of the tab on the Excel spreadsheet. And costLineItems is the name of the array of data objects as shown in the query results above.
A transformation template is required for CSV or Excel output. It is not required for JSON output.
Advanced transformation template example:
```data.{MyReportName: costLineItems[?category != "ALL"].{ AssetId: providerAssetId, Assetname: assetName, CapacityText: capacityText, DataCenter: allTags[?key == 'DataCenter'].value | [0], ServerCategory: allTags[?key == 'ServerCategory'].value | [0], Environment: allTags[?key == 'Environment'].value | [0] }}
The advanced transformation template shown above filters out any data where category equals ```All```.
It also pulls specific values from the ```allTags``` array into named columns. For example, the ```DataCenter``` column in the transformation template is populated by a value from the allTags array where the tag key ```== 'DataCenter'.
Grouped_data transformation template:
data.{MyReportName: costLineItemsWithDRGs.{
DeliveryPlatform: grouped_data.DeliveryPlatform,
Invoiceable: grouped_data.Invoiceable,
Toolname: grouped_data.Toolname
}}
Transformation template for multiple queries:
data.{
sourceDataCosts: myQueryA[].{
category: grouped_data.category,
cost: grouped_data.ccCustomerTotalCostConverted
},
sourceDataAssets: myQueryB[].{
category: grouped_data.category,
cost: grouped_data.ccCustomerTotalCostConverted
}
}
In this case
sourceDataCosts
and
sourceDataAssets
will be the labels of the Source Data tabs in the Excel spreadsheet, if you choose Excel as the output format.
Please review the documentation at (JMESPath)[
https://jmespath.org/], to learn more about advanced transformation template capabilities.
Note:
If your transformation template contains a syntax error, then the query results will not be transformed. JMESPath transformation templates require commas between each expression, but no trailing commas, as shown in the above examples.