IoT Platform provides a parser that uses syntax similar to JavaScript. Compared with SQL expressions, the parser can process complex data and achieve interactions with other cloud services. The parser is used to obtain message content, convert data formats, and forward data. The parser can process strings, JSON-formatted data, and binary data. This topic describes how to write a parser script.

Background information

IoT Platform processes and transmits data based on data formats of topics. For more information about the data formats, see Data formats.

Sample script

The following property data is submitted:

{
    "deviceType": "CustomCategory",
    "iotId": "JCp9***",
    "requestId": "1626948228247",
    "checkFailedData": {

    },
    "productKey": "a1o***",
    "gmtCreate": 1626948134445,
    "deviceName": "Device1",
    "items": {
        "Temperature": {
            "value": 38,
            "time": 1626948134319
        },
        "Humidity": {
            "value": 25,
            "time": 1626948134319
        }
    }
}

The following script is used to parse and process the submitted data:

// Use the payload() function to obtain the data that is submitted by devices and convert the data to JSON-formatted data. 
var data = payload("json"); 
// Filter the submitted temperature and humidity values. 
var h = getOrNull(data, "items", "Humidity", "value");
var t = data.items.Temperature.value;
// Configure a rule. If the temperature value is greater than 38, the rule is triggered to send data to ApsaraDB RDS. 
// An ApsaraDB RDS table includes the following columns: id (auto-increment primary key), deviceName, temperature, humidity, and time. You can call the writeRds() method and specify multiple <column name>:<value> pairs in the method to write the values to the related columns. 
if (t > 38) { 
    writeRds(1000, {"devcieName":deviceName(), "temperature":t, "time":timestamp(), "humidity":h});  
}

The source data that is parsed and processed must be converted to JSON arrays or nested JSON data.

You can use JSONPath expressions or the getOrNull() function in a script to obtain field values. For more information, see LanguageManual UDF and getOrNull().

For example, you can use getOrNull(data, "items", "Humidity", "value"); to obtain the value 25, use data.items.Temperature.value to obtain the value 38, and use data.iotId to obtain the value JCp9***.

Notice If the field that you want to query does not exist, the following logic is implemented.
  • If you use the function, the value null is returned and the script can continue to run.
  • If you use a JSONPath expression, a null pointer appears and the script stops running.

Identifier

You must use identifiers to define constants, variables, and custom fields in the code. Each identifier can contain uppercase and lowercase letters, digits, and underscores (_). An identifier cannot start with a digit.

The following keywords and reserved words cannot be used as identifiers:

  • Keywords: for, break, continue, if, else, true, false, var, new, null, and return.
  • Reserved words: breakdo, instanceof, typeof, case, catch, finally, void, switch, while, debugger, function, this, with, default, throw, delete, in, try, as, from, classenum, extends, super, const, export, import, await, implementslet, let, private, public, interface, package, protected, static, and yield.

Data types

The supported data types of constants, variables, and custom fields in code include number, Boolean, string, byte, map, and array.

The value of a constant can be null. The supported data types of numeric constants include decimal integer, hexadecimal integer, and floating point.

Process control statements

IoT Platform supports for and if...else statements. for statements support the break and continue keywords.

Notice If you use a for statement to repeatedly execute a data forwarding function, the number of loops cannot exceed 100. For more information about data forwarding functions, see Forward data to destinations.

Operator

  • Logical operators: && and .

    For non-Boolean values that are specified in logical conditions, a value of null indicates false and other values indicate true. For example, null && "x" returns false, and null "x" returns true.

  • Mathematical operators: *, /, %, +, and-.

    Only numeric data types are supported. Otherwise, an error occurs.

  • Comparison operators: >, =>, <, <=, ==, and !=. The == comparison operator supports only numeric values.

Comments

Multi-line comments (/* ${comments}*/) and single-line comments (// ${comments}) are supported.

References

  • For information about supported functions, see Functions.
  • For more information about the examples on how to configure parser scripts to forward data, see the documentation in the Data forwarding examples directory.