You need to submit a data parsing script in the IoT Platform console. When a device reports data, the device can send the data to a custom topic that has the parsing tag ?_sn=default. After IoT Platform receives the data, IoT Platform calls the data parsing script, converts the data in the custom format into a JSON structure, and then forwards the JSON-formatted data to the subsequent business system.

Usage notes

  • The parsing of data that is sent to custom topics is supported only in the China (Shanghai) region.
  • The parsing of data that is sent to custom topics is supported only for devices that are connected by using Message Queuing Telemetry Transport (MQTT).
  • IoT Platform parses only data that is reported 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 reported to the cloud and returns the payloads of the parsed data.
  • After IoT Platform parses data, the data still resides in the same topic as that before the parsing. For example, a device sends data to the /${productKey}/${deviceName}/user/update topic, and the parsed data still resides in this topic.

Parsing tag

When you configure a device to publish messages to a custom topic, append the parsing tag ?_sn=default to the custom topic. IoT Platform parses data only when the custom topic to which the data is sent has the parsing tag.

For example, 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. In the left-side navigation pane, choose Devices > Products.
  3. On the Products page, find the product that you want to manage and click View in the Actions column.
  4. On the Product Details page, click the Data Parsing tab.
  5. Select a programming language and write a script in the Edit Script field.

    Only JavaScript (ECMAScript 5), Python 2.7, and PHP 7.2 are supported.

    In the script, define one of the following functions based on your selected programming language:

    • JavaScript (ECMAScript 5): transformPayload()
    • Python 2.7: transform_payload()
    • PHP 7.2: transformPayload()

    For information about the complete sample code, see JavaScript sample script, Python sample script, and PHP sample script.

    Note If the Data Type parameter of the product is set to Custom, you also need to write a script for parsing Thing Specification Language (TSL) data. For information about how to write a script for parsing TSL data, see Example for parsing TSL data.
  6. 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 reports and click Run.
  7. After you confirm that the script is valid, click Submit to submit the script to IoT Platform.