JSONtransforms
With this transform type, you can convert JSON content to requests and responses that are compatible with the Spark Execute API. JSONata is a "lightweight query and transformation language for JSON data". You can test your JSONata transforms in the JSONata Exerciser.
We recommend this transform type if you have a complex JSON transformation that you would like to perform on a single request and response.
Transform document
Transform documents are used to define the code relating to a transform. For JSONtransforms, the transform document is a JSON document defined by the following objects:
transform_type
JSONtransforms_v1.0.0 or JSONtransforms_v1.0.1. Note the JSONata package versions used in Update roadmap.
target_api_version
Optional parameter.
This can be either v3 or v4 depending on whether to target the Execute API (v3) or Execute API (v4).
If omitted, then the default is v3.
input_body_transform
Optional parameter.
Single string containing the code for the input transform. Any double quotation marks " need to be escaped, where " is replaced with \". This will be processed against the request body to the /transforms endpoint.
If omitted, no transformation will take place.
The result will automatically be put under the Execute API inputs object.
output_body_transform
Optional parameter.
Single string containing the code for the output transform. Any double quotation marks " need to be escaped, where " is replaced with \".
If omitted, no transformation will take place.
This will be processed against the response body of the Execute API.
Create the transform document
The transform document for JSONtransforms can be constructed through the Spark UX through the Folder overview.
The document can also be manually created and uploaded to Spark using the New transforms feature.
The
transform_typeisJSONata_v1.0.1.Since we are using Execute API (v3), the
target_api_versionisv3.Both the
input_body_transformandoutput_body_transformmust be a single string containing the code for the transform.Remove any lines containing comments.
Any double quotation marks
"need to be escaped, where"is replaced with\".Note that JSONata allows for single quotation marks
'to be interchanged with double quotation marks". If'are used exclusively in lieu of", then the escape character is not needed.
If you are using Visual Studio Code, you can use the Join lines feature to make it easy to combine multiple lines into a single string.
Select the transform text that you would like to combine onto a single line.
Access the Command Palette with the keyboard shortcut
F1orCtrl+Shift+Pon Windows orShift+⌘+Pon Mac.Type in the command Join lines and select it.
As an alternative to Join lines, line breaks
Ctrl+Entercan be replaced with the new line character\n.
Save the document as a
JSONfile. The filename must end with_transform.json.Follow the instructions in New transform to upload the transform document to Spark.
Sample transform document
Transforms API
Authorization
Bearer {token}accessible from Authorization - Bearer token or systematically via Client credentials.The request headers should include a key for
Authorizationwith the valueBearer {token}.
API key created from Authorization - API keys.
The request headers should include the keys
x-synthetic-keyandx-tenant-namewith the values of the API key and tenant name respectively.
Note that the authorization must include permissions that can access:
The requested Spark service.
The requested transform.
Either the folder that the transform is stored OR
If the transform is in the
apps/transformsfolder. This would typically requireuser:pforsupervisor:pf.
Path parameters
tenant *
Tenant is part of your sign-in URL and also available in the User menu.
transform_folder *
Folder associated with the transform document of the request.
transform *
Transform document associated with the transform document of the request (without _transform.json).
folder *
Folder associated with the Spark service of the request.
service *
Service associated with the Spark service of the request.
Request headers
Meta parameters are included in the request headers to keep the request body as generic as possible to conform to the calling system format.
Content-Type
application/json
x-meta
Optional parameter.
If using target_api_version v4, this should contain a single string containing a JSON object with key value pairs of the Execute API (v4) Request body parameters. Any double quotation marks " need to be escaped and " is replaced with \".
Example:
"{\"version_id\":\"86da8bea-b900-4bf2-8bcf-6a365ac91a4d\"}"
x-request-meta
Optional parameter.
If using target_api_version v3, this should contain a single string containing a JSON object with key value pairs of the Execute API (v3) request_meta parameters. Any double quotation marks " need to be escaped and " is replaced with \".
Example:
"{\"call_purpose\":\"Transform mainframe request\"}"
x-transforms-path
Folder name if the transform document is stored in a folder and the URI pattern does not include transform_folder.
Request body
Content-Type: application/json
HTTP status code
By default, the status code for a successful transformation will be 200. You can also ask the transform to return a custom status code that can be consumed by your application.
If the transformation was successful and the completed output transform includes the key spark_transform_response_httpstatuscode then Spark will use the value of this key as the returned status code.
For example:
This would result in the HTTP status code returned as 422 Unprocessable Entity
Response headers
Content-Type
application/json
x-meta
Optional parameter.
If using target_api_version v4, this should contain a single string containing a JSON object with key value pairs of the Execute API (v4) Request body parameters. Any double quotation marks " need to be escaped and " is replaced with \".
Example:
"{\"version_id\":\"86da8bea-b900-4bf2-8bcf-6a365ac91a4d\"}"
x-request-meta
Optional parameter.
If using target_api_version v3, this should contain a single string containing a JSON object with key value pairs of the Execute API (v3) request_meta parameters. Any double quotation marks " need to be escaped and " is replaced with \".
Example:
"{\"call_purpose\":\"Transform mainframe request\"}"
Response body
Content-Type: application/json
If the Spark Execute API encounters an error, the error from the Execute API will be returned.
Limitations
The API call has a maximum lifetime of
270 s.
Example
The calling system would like to perform addition and multiplication operations but it is in a different format to the Spark service. The Spark service has its inputs and outputs defined as:
This example uses Execute API (v3).
Xoutput_add=Xinput_add_1+Xinput_add_2Xoutput_multiply=Xinput_multiply_1*Xinput_multiply_2For the Spark Execute API (v3) call, we also want to note the
call_purpose.In the response, we would like to include the response_meta
call_id.
The request is transformed to match the Spark Execute API (v3) format and the response is transformed to match the required system response.
HTTP 200 OK Content-Type: application/json
Sample JSONata
Convert JSON "records" table to array table
Convert items with index suffices into an array of objects
Select items with the index suffices _# identified in number_of_plans.
Developer tools
For JavaScript developers, you can try the Node Transform Document Builder which facilitates the build and validation of transform documents. It includes support for JSONata.
Last updated
