Data format conversion using helper functions
Tyk provides two helper functions to assist with data format translation between JSON and XML:jsonMarshalperforms JSON style character escaping on an XML field and, for complex objects, serialises them to a JSON string (example)xmlMarshalperforms the equivalent conversion from JSON to XML (example)
- the use of
.in the template refers to the entire input, whereas something like.myFieldrefers to just themyFieldfield of the input - the pipe
|joins together the things either side of it, which is typically input data on the left and a receiving command to process the data on the right, such asjsonMarshal
{{ . | jsonMarshal }} will pass the entire input to the jsonMarshal helper function.
Using functions within Go templates
You can define and use functions in the Go templates that are used for body transforms in Tyk. Functions allow you to abstract common template logic for cleaner code and to aid reusability. Breaking the template into functions improves readability of more complex tenplates. Here is an example where we define a function calledmyFunction that accepts one parameter:
Additional resources
Here’s a useful blog post and YouTube tutorial that can help you to learn about using Go templates.Go templating examples
Here we provide worked examples for both JSON and XML formatted inputs. We also explain examples using the jsonMarshal and xmlMarshal helper functions.Example JSON transformation template
Imagine you have a published API that accepts the request listed below, but your upstream service requires a few alterations, namely:- swapping the values of parameters
value1andvalue2 - renaming the
value_listtotransformed_list - adding a
user-idextracted from the session metadata - adding a
client-iplogging the client IP - adding a
req-typethat logs the value provided in query parametertype
- Session metadata
uid=user123 - IP address of calling client =
192.0.0.1 - Query parameter
type=strict
.value1accesses the “value1” field of the input JSON- we swap value1 and value2
- we use the range function to loop through the “value_list” array
._tyk_meta.uidinjects the “uid” session metadata value._tyk_context.remote_addrinjects the client IP address from the context._tyk_context.request_data.param.typeinjects query parameter “type”
Example XML transformation template
XML cannot be as easily decoded into strict structures as JSON, so the syntax is a little different when working with an XML document. Here we are performing the reverse translation, starting with XML and converting to JSON. Input- Session metadata
uid=user123 - IP address of calling client =
192.0.0.1 - Query parameter
type=strict
.data.body.value1accesses the “value1” field of the input XML- we swap value1 and value2
- we use the range function to loop through the “value_list” array
._tyk_meta.uidinjects the “uid” session metadata value._tyk_context.remote_addrinjects the client IP address from the context._tyk_context.request_data.param.typeinjects query parameter “type”
XML to JSON conversion using jsonMarshal
ThejsonMarshal function converts XML formatted input into JSON, for example:
Input
JSON to XML conversion using xmlMarshal
ThexmlMarshal function converts JSON formatted input into XML, for example:
Input