JSONtransforms

circle-exclamation

With the Transforms API we are able to support many types of transform definitions. This document describes the transform type JSONtransforms.

In this transform we are able to convert JSON content to requests and responses that are compatible with the Spark Execute API. JSONataarrow-up-right is a "lightweight query and transformation language for JSON data". You can test your JSONataarrow-up-right transforms in the JSONata Exerciserarrow-up-right.

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:

Key
Value

transform_type

JSONtransforms_v1.0.1 uses JSONataarrow-up-right 1.8.7arrow-up-right.

JSONtransforms_v1.0.0 uses JSONataarrow-up-right 2.1.0arrow-up-right.

starget_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, i.e. " 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. See the Example for more details.

output_body_transform

Optional parameter. Single string containing the code for the output transform. Any double quotation marks " need to be escaped, i.e. " is replaced with \". If omitted, no transformation will take place. This will be processed against the response body of the Execute API. See the Example for more details.

Build transforms

To build transforms you can follow the instructions for the specific transform on this page. For JavaScript developers there is also the Node Transform Document Builderarrow-up-right that can be used to assist with the development.

Transforms API

These details are specific to this transform in addition to information in Transforms API.

Request headers

In addition to the Transforms API Request headers, these are specific to this transform.

Meta parameters are included in the request headers to keep the request body as generic as possible to conform to the calling system format.

Key
Value

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, i.e. " 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, i.e. " is replaced with \". Example: "{\"call_purpose\":\"Transform mainframe request\"}"

Request body

  • Content must be in JSON.

Limitations

  • The API call has a maximum lifetime of 29 s.

  • The API request can include a maximum payload size of 6 MB.

Response headers

Key
Value

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, i.e. " 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, i.e. " is replaced with \". Example: "{\"call_purpose\":\"Transform mainframe request\"}"

Response body

  • Content will be in JSON.

  • If the Spark Execute API encounters an error, the error from the Execute API will be returned.

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

Example

In this 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:

  • For this example we will be using the Execute API (v3).

  • Xoutput_add = Xinput_add_1+Xinput_add_2

  • Xoutput_multiply = Xinput_multiply_1*Xinput_multiply_2

  • For the Spark Execute API (v3) call, we also want to note the call_purpose.

  • In the response, we would like to include the Execute API (v3) response_meta call_id.

file-download
9KB

The below illustrates how the request is transformed to match the Spark Execute API (v3) format and how the response is transformed to match the required system response.

Request
JSONata transform
Transformed request
Spark response
JSONata transform
Transformed response

Create the transform document

The transform can be assembled into the transform document as attached below.

  1. Follow the instructions in Create new transform documents to create a new transform in the folder of your choice.

  2. The Transform type is JSONata_v1.0.1.

  3. Since we are using Execute API (v3), choose Target API version as v3.

  4. Copy and paste the JSONata transforms above in to the Request transform and Response transform respectively.

Alternatively the document can be manually created and uploaded to Spark using the New transforms feature.

  1. The transform_type is JSONata_v1.0.1.

  2. Since we are using Execute API (v3), the target_api_version is v3.

  3. Both the input_body_transform and output_body_transform each must be a single string containing the code for the transform.

    1. Remove any lines containing comments.

    2. Any double quotation marks " need to be escaped, i.e. " is replaced with \".

      • Note that JSONataarrow-up-right allows for single quotation marks ' to be interchanged with double quotation marks ". If ' are used exclusively in lieu or ", then the escape character is not needed.

    3. If you are using Visual Studio Codearrow-up-right, you can use the Join lines feature to make it easy to combine multiple lines into a single string.

      1. Select the transform text that you would like to combine onto a single line.

        1. Access the Command Palette F1 or Ctrl+Shift+P on Windows or Shift+⌘+P on Mac.

        2. Type in the command Join lines and select it.

    4. As an alternative to Join lines, line breaks Ctrl+Enter can be replaced with the new line character \n.

  4. Follow the instructions on Transform documents to upload the transform document to Spark.

Sample request

Sample response

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.

Last updated