Write custom scripts

Updated at:

You can write custom JavaScript (ECMAScript 5) scripts in IoT Platform to parse data from cloud product data sources with customized field mapping. This topic provides script templates and examples.

Script template

// Below is a script template to guide your script writing process

/**
 * Function name must be parse_line
 * Input parameter: line - row data, such as a,b,c
 * Output parameter: jsonObj - a JSON object, which should not be empty
 */
function parse_line(line) {
  var lines = line.split(",");
  return {"a":lines[0],"b":lines[1],"c":lines[2]};
}

In this template, a, b, and c are the field names in the row data.

Script example

// Example data

/*
  Input line row data:
      32,60     
  Output JSON:
      {
         "temperature":32,
         "humidity":60
      }
 */
function parse_line(line) {
  var lines = line.split(",");
  return {"temperature":lines[0],"humidity":lines[1]};
}