A device submits data in a custom format to a custom topic that has the parsing tag ?_sn=default. After IoT Platform receives the data, IoT Platform calls a data parsing script, converts the data in a custom format to a JSON structure, and then forwards the JSON-formatted data to subsequent business systems. This topic describes how to write a script to parse data in a custom format and submit the script in the IoT Platform console.

Usage notes and limits

  • IoT Platform allows you to parse data from custom topics only in the China (Shanghai) and Japan (Tokyo) regions.
  • IoT Platform parses only data from custom topics that is submitted by devices that are connected over Message Queuing Telemetry Transport (MQTT).
  • IoT Platform parses only data that is submitted by devices to the cloud, and does not parse downstream data that is sent from the cloud.
  • IoT Platform parses the payloads of data that is submitted to the cloud and returns the payloads of the parsed data.
  • When IoT Platform parses data, the parsed data resides in the same topic. For example, a device sends data to the /${productKey}/${deviceName}/user/update topic, and the parsed data resides in this topic.

Parsing tag

When you configure a device to publish messages to a custom topic, attach the parsing tag ?_sn=default to the custom topic.

Warning IoT Platform parses data only when the custom topic to which the data is sent has the parsing tag.

For example, if you want to parse the data that is sent by a device to the /${productKey}/${deviceName}/user/update topic into JSON-formatted data, you must specify the following topic when you configure the device: /${productKey}/${deviceName}/user/update?_sn=default.

Note When you create a custom topic in the IoT Platform console, do not append the parsing tag to the topic.

Procedure

  1. Log on to the IoT Platform console.
  2. On the Overview page, find the instance that you want to manage and click the instance name to go to the Instance Details page.
    Important Enterprise Edition instances are available in the China (Shanghai) and Japan (Tokyo) regions. If the Enterprise Edition instances are unavailable in the region that you select, skip this step.
    Overview
  3. In the left-side navigation pane, choose Devices > Products.
  4. On the Products page, find the product that you want to manage and click View in the Actions column.
  5. On the Product Details page, click the Data Parsing tab.
  6. Select a programming language and write a script in the Edit Script field.
    Supported scripting language Required function Sample code
    JavaScript (ECMAScript 5) transformPayload() JavaScript sample script
    Python 2.7 transform_payload() Python sample script
    PHP 7.2 transformPayload() PHP sample script
    Note If the Data Type parameter of the product is set to Custom, you also need to write a script to parse Thing Specification Language (TSL) data. For information about how to write a script to parse TSL data, see Submit a data parsing script.
  7. Test the script.
    1. On the Input Simulation tab, set the Simulation Type parameter to Custom and select a device and a topic.
    2. Enter simulated data that a device submits and click Run.
  8. After you confirm that the script is valid, click Submit to submit the script to IoT Platform.

Optional: Update the data parsing script

After you submit the data parsing script, go to the Data Parsing tab. On the Data Parsing tab, modify or delete script code and then re-submit the script.

Warning After you modify or delete script code and re-submit the script of a product, the data parsing service may become unavailable for the devices in the product. As a result, your business may be interrupted. Proceed with caution.

References