Skip to main content

JavaScript transform tutorials

Tutorials for the JavaScript transform method

This document provides tutorials for using the JavaScript transform method, specifically for recalculating Celsius temperatures as Fahrenheit. It details creating a JavaScript code package to convert the temperature, setting up an integration table transform that utilizes this package, creating an integration table, inserting a record with a Celsius temperature, and querying the results to see the calculated Fahrenheit temperature. The tutorial outlines the JSON payloads for each step, showing how the data flows and is transformed.

JavaScript transformtransform tutorialJavaScript tutorialJavaScript transform tutorialdata transformationcode packageAPIJSONtutorialsintegration table

This tutorial creates a JavaScript code package to read a "temperatureCelsius" property out of a JSON document in the source_payload field. It assumes the temperature value is Celsius, converts it to Fahrenheit, and writes the result to the temperature_fahrenheit field.

Create a JavaScript code package

// integrationTableTransform returns a modified record
// Server sets the record variable before running the code package
try {
  
  let temperatureCelsius = record.source_payload.temperatureCelsius;
  let temperatureFahrenheit = (temperatureCelsius * 1.8) + 32;
  
  record.temperature_fahrenheit = temperatureFahrenheit;
  
} catch (err) {
  record.transformError = err.message;
}

The code package uses JavaScript to read a "temperatureCelsius" property out of the JSON document in the source_payload field. It assumes the temperature value is Celsius, converts it to Fahrenheit, and writes the result to the temperature_fahrenheit field. When you assign this transform to an integration table, the server automatically creates the temperature_fahrenheit field if it does not already exist.

{
  "api": "admin",
  "apiVersion": "1.0",
  "action": "createCodePackage",
  "params": {
    "databaseName": "faircom",
    "ownerName": "admin",
    "codeName": "convertCelsiusToFahrenheit",
    "newCodeName": "convertCelsiusToFahrenheit",
    "codeLanguage": "javascript",
    "codeFormat": "utf8",
    "codeType": "integrationTableTransform",
    "codeStatus": "testing",
    "description": "Converts the value of the \"temperature_celsius\" field to Fahrenheit and stores the result in the temperature_fahrenheit field.",
    "metadata": {
      "defaultIntegrationTableName": "mod1",
      "inputFields": [
        "source_payload"
      ],
      "outputFields": [
        "temperature_fahrenheit"
      ],
      "outputFieldDefinitions": [
        {
          "name": "temperature_fahrenheit",
          "type": "integer"
        }
      ]
    },
    "comment": "",

    "code": "// integrationTableTransform returns a modified record\n// Server sets the record variable before running the code package \ntry {\n\n    let temperatureCelsius = record.source_payload.temperatureCelsius;\n    let temperatureFahrenheit = (temperatureCelsius * 1.8) + 32;\n\n    record.temperature_fahrenheit = temperatureFahrenheit;\n\n} catch (err) {\n    record.transformError = err.message;\n}"

  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

Create an integration table transform

This transform contains one step, which runs the previously created JavaScript code package. 

It specifies that the transform requires the temperature_fahrenheit field to store its results. When you assign this transform to an integration table, the server automatically creates the temperature_fahrenheit field if it does not already exist.

{
  "api": "transform",
  "apiVersion": "1.0",
  "action": "createTransform",
  "params": {
    "transformName": "Tutorial: Convert Celsius to Fahrenheit",
    "transformActions": [
      {
        "transformStepName": "JavaScript Transform: Convert Celsius to Fahrenheit ",
        "transformStepMethod": "javascript",
        "transformService": "v8TransformService",

        "inputFields": [
          "source_payload"
        ],
        "outputFields": [
          "temperature_fahrenheit"
        ],

        "transformParams": {
          "codeName": "convertCelsiusToFahrenheit",
          "databaseName": "faircom",
          "ownerName": "admin",
          "outputFieldDefinitions": [
            {
              "name": "temperature_fahrenheit",
              "type": "integer"
            }
          ]
        }
      }
    ]
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

Create an integration table

Create an integration table and connect it to a JavaScript transform that runs each time you insert a record.

{
  "api": "hub",
  "apiVersion": "1.0",
  "action": "createIntegrationTable",
  "params": {
    "tableName": "test1",
    "ownerName": "admin",
    "databaseName": "faircom",

    "transformName": "Tutorial: Convert Celsius to Fahrenheit"
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

Insert a record

Insert a record into the table to run the transform on that record and populate the temperature_fahrenheit field.

Notice that we are only inserting a value into the source_payload field.

{
  "api": "db",
  "apiVersion": "1.0",
  "action": "insertRecords",
  "params": {
    "tableName": "test1",
    "dataFormat": "objects",
    "sourceData": [
      {
        "source_payload": { "temperatureCelsius": 20 }
      }
    ]
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

Query the results

{
  "api": "db",
  "apiVersion": "1.0",
  "action": "getRecordsByTable",
  "params": {
    "tableName": "test1"
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

Results

The temperature_fahrenheit field contains 68. When you inserted the record, the server automatically ran the JavaScript transform, which calculated the fahrenheit value from "temperatureCelsius" in the source_payload field and updated the temperature_fahrenheit field.

 "data": [
    {
      "source_payload": { "temperatureCelsius": 20 },
      "temperature_fahrenheit": 68,
      
      "create_ts": "2025-05-08T00:46:35.166",
      "changeId": 16128,
      "error": false,
      "log": null,
      "id": 1
    }
  ]