Data format conversion using helper functions
Tyk provides two helper functions to assist with data format translation between JSON and XML:jsonMarshal
performs JSON style character escaping on an XML field and, for complex objects, serialises them to a JSON string (example)xmlMarshal
performs the equivalent conversion from JSON to XML (example)
- the use of
.
in the template refers to the entire input, whereas something like.myField
refers to just themyField
field 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
value1
andvalue2
- renaming the
value_list
totransformed_list
- adding a
user-id
extracted from the session metadata - adding a
client-ip
logging the client IP - adding a
req-type
that logs the value provided in query parametertype
- Session metadata
uid
=user123
- IP address of calling client =
192.0.0.1
- Query parameter
type
=strict
.value1
accesses 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.uid
injects the “uid” session metadata value._tyk_context.remote_addr
injects the client IP address from the context._tyk_context.request_data.param.type
injects 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.value1
accesses 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.uid
injects the “uid” session metadata value._tyk_context.remote_addr
injects the client IP address from the context._tyk_context.request_data.param.type
injects 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