IoT Hub Microsoft Azure Syntax

#1

I am trying to get my data into my IoT Hub on Azure from a Modbus signal.

My flow is as follows:

[{"id":"a40d774a.bf8648","type":"azureiothub","z":"53f246d7.f619f8","name":"Azure IoT Hub","protocol":"mqtt","x":780,"y":120,"wires":[[]]},{"id":"99bdb067.08498","type":"function","z":"53f246d7.f619f8","name":"","func":"msg.payload = msg.payload.split(\",\");\nmsg1 = '{\"deviceID\": \"WaveBuoy\", '\nmsg1 = msg1 + '\"SAK\": \"PrimaryKey=\", ' \nmsg1 = msg1 + '\"Protocol\": \"mqtt\", '\nmsg1 = msg1 + '\"Data\": { \"' + msg.topic + '\": \"' + msg.payload[4] + '\": \"' + msg.payload[5] + '\": \"'+ msg.payload[6] + '\"}}'\n\nnewMsg = { payload: msg1 };\nreturn newMsg;\n\nmsg1 = msg1 + '\"Data\": { \"' + msg.topic + '\": \"' + msg.payload + '\"}}'","outputs":1,"noerr":0,"x":370,"y":280,"wires":[["4073cbfd.cf8d14","a40d774a.bf8648"]]},{"id":"beb2fc2f.9da3b","type":"tcp in","z":"53f246d7.f619f8","name":"","server":"client","host":"xxx","port":"xxx","datamode":"stream","datatype":"utf8","newline":"","topic":"WaveBuoy","base64":false,"x":150,"y":280,"wires":[["99bdb067.08498"]]}]

I am getting the following error:

msg : error
"SyntaxError: Unexpected token : in JSON at position 129 or 128

My string looks like this:

"{"deviceID": "WaveBuoy", "SAK": "PrimaryKey=", "Protocol": "mqtt", "Data": { "WaveBuoy": "0.01": "0.02": "0.09"}}"

If anyone could aid me it would be very much appreciated.

#2

The problem is exactly what it says - at character 129 of your string there is an unexpected :.
Reformatting it slightly will make it clearer:

{
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
      "WaveBuoy": "0.01": "0.02": "0.09"
   }
}

The WaveBuoy property is not valid JSON. Should it be an array of values? Or should 0.02 and 0.09 be separate properties with they own key name?

#3

The latter, separate properties, they represent vertical, northings and eastings.

I see, I think. How does one put as many variable in the data bit as I would like?

#4

Do you mean something like:

{
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
      "WaveBuoy": {
         "vertical": "0.01",
         "northings": "0.02",
         "eastings": "0.09"
      }
   }
}
#5

Exactly what I mean! Apologies if I was unclear. Or even this.

{
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
         "vertical": "0.01",
         "northings": "0.02",
         "eastings": "0.09"
      }
   }
}
#6

Currently you are manually building up the JSON string. That is quite an error-prone way of doing it. You can build up the equivalent JavaScript object and convert it to JSON at the end of the process.

var parts = msg.payload.split(",");

msg.payload = {
   "deviceID": "WaveBuoy",
   "SAK": "PrimaryKey=",
   "Protocol": "mqtt",
   "Data": {
      "vertical": msg.payload[4],
      "northings": msg.payload[5] 
      "eastings": msg.payload[6] 
   }
};

msg.payload = JSON.stringify(msg.payload);

return msg;

You could leave out the JSON.stringify line and then wire this node to a JSON node to do the conversion.

Nick

#7

In the interests of saving wear on the fingers you don’t need quotes round the keys unless they contain special characters or are otherwise capable of misinterpretation. So you could have

msg.payload = {
   deviceID: "WaveBuoy",
   SAK: "PrimaryKey=",
   Protocol: "mqtt",
   Data: {
      vertical: msg.payload[4],
      northings: msg.payload[5],
      eastings: msg.payload[6] 
   }
};

By the way there is a missing comma on the end of the northings line.

#8

I have carried out your correction, along with the changes that you stated in my other thread. However It still doesn’t seem to work…

Have you transmitted data to an IoT Hub on Azure?

#9

Oh, did I post on a different thread? I can’t find that one now.

No I have not used the node, I was just comparing what you have with the readme for the node. Post what you have in the function node now and show us what the output looks like in a debug node. Check what you see with the readme for the node.