IoT Platform provides a parser that uses similar syntax as 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 article 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 by using the JSON format. 
var data = payload("json"); 
// Filter the submitted temperature and humidity values. 
var h = data.items.Humidity.value;
var t = data.items.Temperature.value;
// Set a rule to send data to ApsaraDB RDS if the temperature value is greater than 38. 
// An ApsaraDB RDS table includes the following columns: id (auto-increment primary key), deviceName, temperature, humidity, and time. You can use the writeRds() method to write values to columns by using the format of column:value. 
if (t > 38) { 
    writeRds(1000, {"devcieName":deviceName(), "temperature":t, "time":timestamp(), "humidity":h});  
}
Notice The data that is submitted in the JSON format is an array or nested JSON data. You can use JSONPath expressions in a parser script to obtain property values. For example, data.items.Humidity.value is specified in the preceding script to obtain the humidity value 25. data.iotId is specified to obtain the instance ID JCp9***.

For more information about JSONPath expressions, see LanguageManual UDF.

Identifiers

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 (_). It 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 data types of constants, variables, and custom fields in the code can be number, Boolean, string, byte, map, and array.

The value of a constant can be null. The data types of numeric constants can be 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.

Operators

  • Logical operators: && and ||.

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

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

    Only the numeric data type is supported. Otherwise, an error occurs.

  • Relational operators: >, =>, <, <=, ==, and !=. The equality operator (==) supports only the comparison of key values.

Comments

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

References

For information about available functions, see Functions.