Script and output syntax
Scripts allow you to parse messages, transform data formats, process strings, assemble JSON data, and handle binary data. The output file uses a fixed JSON array format to map data from device reports or script parsing to digital twin nodes. This topic describes how to write scripts and output files.
Background information
The following types of data sources can be added to a digital twin:
| Type | Description |
| Topic data |
|
| API data | IoT Platform processes and transmits data based on the JSON data passed in the Items.N.Params request parameter of the ImportDTData |
IoT Platform provides scripts and output files to map data sources to digital twins. The scripts are written in a language similar to JavaScript and must follow JavaScript syntax.
IoT Platform scripts support a subset of JavaScript syntax and a limited set of regular expressions. For more information, see Regular expressions and Script syntax.
Scripts and output files
Script file
transformScript.txt. A script is typically written as follows:- In the script file, use the
payload()Items.N.Paramsvar payload = payload("json"); - The script must output JSON key-value pairs. To do this, define a Map object to store the data.
var data = {}; - Retrieve the property values from the payload. Process and calculate the data as required, and then store the results in the data object.
- Return the parsed data.
return data;
Output file
destConfig.json. The following code provides a template: [
{
"key": "key1 from the script output, or key1 from the payload of the source topic, API, or data parsing configuration",
"iotId": "Digital twin node ID 1",
"identifier": "Identifier of the TSL model for digital twin node 1",
"name":"Name of the digital twin node that corresponds to digital twin node ID 1"
},
{
"key":"key2 from the script output, or key2 from the payload of the source topic, API, or data parsing configuration",
"iotId": "Digital twin node ID 2",
"identifier": "Identifier of the TSL model for digital twin node 2",
"name":"Name of the digital twin node that corresponds to digital twin node ID 2"
}
]Usage notes
- The JSON data from device topic reports, API uploads, and data parsing can be an array or a nested JSON object. You can use JSONPath in script files and output files to retrieve property values from the JSON data.
For example:
- In the script file, use
payload.items.Position.value.latitudeto retrieve the value 39.9. - In the output file, use the
items\\.Position\\.value\\.longitudeformat to retrieve the value 116.38.
For more information about JSONPath, see LanguageManual UDF.
- In the script file, use
- A period (.) can be used as a standard character within identifiers for constants, variables, or other custom fields in a script.
- Use the
deviceName()function to retrieve the name of the device for the data source.If a data source contains data from multiple devices, you can map the data for each device separately by checking the device name. If you map data from all devices to the TSL model of a single digital twin node, the data is overwritten each time a device uploads new data. As a result, the TSL model of the digital twin node displays only the data that was last uploaded.
- If you do not need to process the data and the data source is already in JSON format, you can skip using a script file for parsing. Instead, you can edit the output file directly to perform data mapping.
Script and output examples
This section uses property data reported over a device topic as an example. The following code shows the input data:
{
"iotId":"4z819VQHk6VSLmmBJfrf00107e****",
"productKey":"al12345****",
"deviceName":"deviceName1234",
"gmtCreate":1510799670074,
"deviceType":"Ammeter",
"items":{
"Power":{
"value":"on",
"time":1510799670074
},
"Position":{
"time":1510292697470,
"value":{
"latitude":39.9,
"longitude":116.38
}
}
},
"checkFailedData":{
}
}The following sample script shows how to parse and process the data:
// Use the payload() function to get the message content reported by the device and convert it to the JSON format.
var payload = payload("json");
// Define a Map object to store key-value pairs.
var data = {};
// Get the latitude value of the position, add 1 to it, and store the result in the data object.
data["k1"] = payload.items.Position.value.latitude + 1;
// Convert the value of Power. If the value is "on", convert it to 1. Otherwise, convert it to 0.
if (payload.items.Power.value == "on") {
data["k2"] = 1;
} else {
data["k2"] = 0;
}
// Get the value of items.
data["items"] = payload.items;
// Add a constant named stage1.stage2 to the data object.
data["stage1.stage2"] = 1.3;
// Return the parsed data.
return data;The data object returned by the script serves as the data source for the output file. Configure the data mapping using the JSON format in the following example:
[
{
// The key value corresponds to a field in the data object.
"key": "k1",
// The iotId value corresponds to the ID of the digital twin node.
"iotId": "xx***",
// The property identifier for the digital twin node. This updates the value of the id1 property under the "xx***" node to the value of k1.
"identifier": "id1",
"name":"node1"
},
{
"key": "k2",
"iotId": "yy***",
"identifier": "id2",
"name":"node2"
},{
"key": "items\\.Position\\.value\\.longitude",
"iotId": "yy***",
"identifier": "id3",
"name":"node3"
},{
"key": "stage1.stage2",
"iotId": "zz***",
"identifier": "id4",
"name":"node4"
}
]