I am missing something here with message format - JSON

These are the 3 nodes:

[{"id":"a2b9b7d51d629336","type":"mqtt in","z":"d65ab744.465758","name":"","topic":"ARLEC-1/#","qos":"2","datatype":"auto","broker":"378c0403.8cda04","nl":false,"rap":true,"rh":0,"inputs":0,"x":810,"y":420,"wires":[["e526db181bab789a","28868a5dfe0ca601"]]},{"id":"e526db181bab789a","type":"json","z":"d65ab744.465758","name":"","property":"payload","action":"","pretty":false,"x":990,"y":420,"wires":[["abec26cc161f6cbd"]]},{"id":"28868a5dfe0ca601","type":"debug","z":"d65ab744.465758","name":"RAW ARLEC-1 message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":470,"wires":[]},{"id":"378c0403.8cda04","type":"mqtt-broker","name":"TIMEPI MQTT","broker":"192.168.0.99","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"true","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

This is what I see as the RAW message:

{"topic":"ARLEC-1/tele/LWT","payload":"Online","qos":0,"retain":false,"_msgid":"94f63726e95a2b79"}

and I am seeing this error from the JSON node:

Unexpected end of JSON input

Now, I know I have the JSON node set to autodetect.
I fear I have messed up in the message structure, but can't be sure.

The JSON nodes converts msg.payload, which should be an object or a JSON string. Your payload is just a simple string, "Online".

So what do I do if the payload varies between simple text and complicated stuff?

I'm guessing this problem is biting me in many places because the messages coming in vary from simple stuff to complex/structures payloads.

:confused:

Use Catch node
image

Configure the change node after that, to determine if it has LWT in topic.

So you know that the payload will be string (Online or Offline) and then use that payload as you'll need.

Thanks for that.

Not to dismiss what you said - indulge me - so having only ARLEC-1/# as the topic is asking for trouble?

Would it save me a lot of hurt if I resolved it down another level in the topic?
That way I get around some messages being JSON or plain text.

Why not try ARLEC-1/tele/LWT as topic? I would assume you would then get strings only in the payload

TBH, I am a bit overwhelmed with this.

I think most of the time I have better topic definition but there are a couple of nodes which are like this one here in this example.

I'm not exactly sure why, maybe only for debuging but now and then they complain (well, the JSON node after them) about the message.

Could I get around this problem in a similar way (no offence @hotNipi) with this kind of set up:

and the switch node is set as

If different topics require different processing then use separate MQTT nodes. Then on the ones that are objects you can specify the output as the result of converting the JSON to an object.

Yes. So under the example ARLEC-1/# I am receiving LWT and other messages.
One is plain text and another is a JSON message.

So (going further down this thread) should I resolve the topic better so I separate the different types of messages?

But if I leave the example posted - maybe just to see the messages at the higher level - would that example I posted work? (Though it seems to be.) But I am known to be wrong. :wink:

As I said, if they need different processing (as they do in that case) then use different MQTT nodes, so you can configure one as string and the other as parsed json.

Or the alternative is to use one MQTT node and a Switch node to send the different topics down different paths, whichever is best for your particular system.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.