By default, data points get written with a path derived from the topic on which the message was delivered. In our example device, the name of the device is part of the JSON payload. We would prefer to publish the data points with the device name as part of the point name. To do this, we can extract the device name as part of the parser script, and then use that in the app.PointNameModifier callback to alter the point name the parser produces.
In the DataHub MQTT Broker, reopen the Advanced JSON Message Format window to continue editing.
In the Parser Script, we need to extract the device name and store it in a local variable. This needs to be in the Parser Script, not the Initialization Script because the device name is different for each message, and not known until the message arrives. To do this, add the following line to the Parser Script:
var devicename = input.Json["devicename"];

Add this code to the Initialization Script:
app.PointNameModifier = function (topic, jsonPath)
{
var pointName = app.PointNameFromTopic (topic, devicename +
"/" + jsonPath);
return pointName;
};
Remember, you can find code templates for callbacks, applications, JSON, and more in Common Formulas. Double-click to enter them into the work space.

Click the button. Your test output should now look like this:

The topic.branch.leaf. text is a placeholder in the
test output. It will be replaced by the actual path specified by the
MQTT client.
Click to close the Advanced JSON Message Format window, and then click .
Restart your DataHub instance to clear any unused data points.
Reconnect your MQTT demo client to your DataHub instance and resend the sample message. The Data Browser display should show the data points in the device1 branch of the MQTT-Broker domain.

This may not be exactly what we want either. The
devicename field is sufficient to identify this
device, so we do not want the additional path created by the topic name.
We can remove a single level of the topic before computing the point
names by modifying the app.PointNameModifier to remove the last path element from
the topic:
app.PointNameModifier = function (topic, jsonPath)
{
topic = topic.Delete(-1,1) + "/" + devicename;
var pointName = app.PointNameFromTopic (topic, jsonPath);
return pointName;
};
Restart your DataHub instance to clear any unused data points, and then reconnect your MQTT demo client and send the sample message again. You should now see data points like this:

Now, the default behavior of the system is to insert a quality value of
192 (GOOD) and a timestamp of the current system time.
But you can change those to the quality and timestamp coming from the device
itself. In our example, the values of qcomms and
tstamp contain that information. Here's how you can
extract it and apply it to the data points.
Reopen the Advanced JSON Message Format window to continue editing.
In the Initialization Script, add a
ValueProcessor callback that overrides the standard behaviour of app.ProcessJson when it is provided. We
will use this to write the data point values ourselves, with our own
timestamp and quality:
app.ValueProcessor = function (topic, jsonPath, obj)
{
app.WritePoint (topic, jsonPath, obj, quality,
JsonExtensions.GetDate(timestamp));
};
In the Parser Script, add these lines to assign the values from the device.
var quality = input.Json["qcomms"]; var timestamp = input.Json["tstamp"];

These assign the values for the time stamp and quality coming from the
sample input (input.JSON) to the
timestamp and quality
variables specified in the app.WritePoint function.
Since these values may change for each message, they need to be assigned
in the Parser Script. All of these lines of code
must be written above the final app.ProcessJson();
line.
We use the JsonExtensions.GetDate function to
convert the numerical time stamp coming from the device into a date/time
format.
Click the button. The test output shows that the quality (64 = UNCERTAIN) and timestamp (1633677890 = 2021-10-08 7:24:50 AM) are now coming from the sample input, and are not being generated by the DataHub instance.

Click to close the Advanced JSON Message Format window, and then click .
Reconnect your MQTT demo client to your DataHub instance and resend the sample message. You should now see that the MQTT test client values for time stamp and quality were sent to the DataHub instance.

![]() | |
The multiple-hour difference in time stamps is because the Test Output parser displays dates in GMT0 time, while the DataHub Data Browser displays dates in local time. |