Issues parsing json payloads in MQTT
# umh-support
t
Issues parsing json payloads in MQTT protocol converter...
Here is the processor....
Copy code
# these variables are autogenerated from your instance location
let enterprise = "MACH-Controls-LLC"
let site = "Dallas"
let area = "Booth-18"
let schema = "_local"

# Get MQTT Topic, replacing spaces in topic with _
let topic = meta("mqtt_topic").replace_all(" ", "_")

# Set the tagname to be from the 4th level
# Excluding the last 2 levels. 
# Including any levels between, but joining them with '.'
# By default the last level is not included in the 'slice" function.
let tagname = $topic.split("/").slice(4,-1).join(".")

# Set the payloadTagName to be the last level of the topic
let payloadTagName = $topic.split("/").index(-1)

value = content()
The result....
Copy code
{
  "Production": "{\"timestamp_ms\":1739564659054,\"schema\":\"production\",\"ProdState\":1,\"TotalParts\":6824,\"RejectParts\":370}",
  "timestamp_ms": 1739564659374
}
My solution was to just use node red to republish on the appropriate _historian topic and it consumes the payload perfectly. Something with the bloblang is mucking it up. I believe something in the post processing, after my processor is adding the escape characters, because I can't seem to get rid of them in my processor.
d
you could try to use the javascript language instead of the bloblang? (i have not done it myself) but it should be easier i suppose/maybe you will yield a better result?
t
That’s definitely something I want to explore, I just found out about this being released….but don’t have time to revisit before the conference next week. Regardless, why can’t I just pass in a payload value that already meets the schema and it get consumed appropriately..?
j
you could try passing it on using a mqtt-to-kafka bridge? or in general with a custom data flow compomemt? the bloblang protocol converters dont support it, (just understood that you were not using ndered_js yet)
for nodered_js, use the validate button
then copy paste verything incl. benthos-umh documentation into chatgpt
even 4o will gfive you a good result
t
How do i use a mqtt to kafka bridge for a different mqtt broker as the source? With a custom component?
This is effectively what I did, I just used node red to republish to the umh broker at an _historian topic…. But i was bridging broker to broker.
j
yes!