JavaScript transform tutorials

Tutorials for the JavaScript transform method

Recalculate a Celsius temperature a Fahrenheit

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.

Requirements:

 

Create a JavaScript code package

This example javascript code reads a value in Celsius from the source_payload, converts it to Fahrenheit, and writes the new value to the temperature_fahrenheit field:

// 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 below uses the JavaScript above 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.

  1. Navigate to the Code tab.
  2. Select API Console from the Code menu.
  3. Paste this code into the API Console and click the Run button:
    1. {
        "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.",
          "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 with a transform

  1. Navigate to the Code tab.
  2. Select API Console from the Code menu.
  3. Paste this code into the API Console and click the Run button:
    1. {
        "api": "hub",
        "apiVersion": "1.0",
        "action": "createIntegrationTable",
        "params": {
          "tableName": "test1",
          "fields": [
            {
              "name": "temperature_fahrenheit",
              "type": "double"
            }
          ],
          "transformSteps": [
            {
              "transformStepMethod": "javascript",
              "transformStepService": "v8TransformService",
              "codeName": "convertCelsiusToFahrenheit",
              "ownerName": "admin",
              "databaseName": "faircom"
            }
          ]
        },
        "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.

  1. Navigate to the Code tab.
  2. Select API Console from the Code menu.
  3. Paste this code into the API Console and click the Run button:
    1. {
        "api": "db",
        "apiVersion": "1.0",
        "action": "insertRecords",
        "params": {
          "tableName": "test1",
          "dataFormat": "objects",
          "sourceData": [
            {
              "source_payload": { "temperatureCelsius": 20 }
            }
          ]
        },
        "authToken": "replaceWithAuthTokenFromCreateSession"
      }

 

Query the results

  1. Navigate to the Code tab.
  2. Select API Console from the Code menu.
  3. Paste this code into the API Console and click the Run button:
    1. {
        "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
    }
  ]