# Mosquitto mqtt example without escaping quotes?

**URL:** <https://discourse.nodered.org/t/mosquitto-mqtt-example-without-escaping-quotes/96716>\
**Category:** General\
**Created:** [23 April 2025 10:46 UTC](https://discourse.nodered.org/t/mosquitto-mqtt-example-without-escaping-quotes/96716 "2025-04-23T10:46:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [23 April 2025 10:46 UTC](https://discourse.nodered.org/t/mosquitto-mqtt-example-without-escaping-quotes/96716/1 "2025-04-23T10:46:47Z")

</div>

This is a not exactly a question about node red, but since this community is also expert in related fields, perhaps someone can answer anyway.

I've been playing around with mosquitto\_pub and an example from thingsboard:  
[MQTT Device API Reference | ThingsBoard Community Edition](https://thingsboard.io/docs/reference/mqtt-api/#publish-attribute-update-to-the-server)

```auto
mosquitto_pub -d -h "demo.thingsboard.io" -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN" -m "{"attribute1": "value1", "attribute2": true}"

```

What I don't understand is how un-escaped quotes works in the message (-m flag). It doesn't escape the quotes inside json, nor does it encapsulate with single quotes. Yet this works directly from linux terminal? Chatgpt insists this is not possible, and either the copied text is not the same (something I can't observe, because pasting it in notepad shows it as identical), or that the terminal somehow magically escapes it when pasting (also not the case)?

Perhaps mosquitto is being nice and automatically handles this? But that's kinda unusual behavior for cli programs? Chatgpt also denies this...

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 April 2025 11:09 UTC](https://discourse.nodered.org/t/mosquitto-mqtt-example-without-escaping-quotes/96716/2 "2025-04-23T11:09:23Z")

</div>

Indeed, very odd. At face value, it shouldn't work. I think this gets a classification of "magic". 😉

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 April 2025 11:28 UTC](https://discourse.nodered.org/t/mosquitto-mqtt-example-without-escaping-quotes/96716/3 "2025-04-23T11:28:55Z")

</div>

Are you you sure it works? For me it nearly works, but the string I get back is  
`{attribute1: value1, attribute2: true}`  
which is not valid JSON, all the quotes are missing. How it works at all I don't know. It does work if you use single quotes round the value.  
`-m '{"attribute1": "value1", "attribute2": true}'`

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 April 2025 11:43 UTC](https://discourse.nodered.org/t/mosquitto-mqtt-example-without-escaping-quotes/96716/4 "2025-04-23T11:43:00Z")

</div>

All of these quotes and braces get a thorough going-over by Bash before they arrive at mosquitto\_pub, and what comes out of the wringer does as Colin says, _sort of_ work.  
I think it's related to how Bash interprets squiggly brackets, though it seems to work in Dash too.

You should definitely protect the quotes in your json by wrapping the message inside single quotes.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 July 2025 11:43 UTC](https://discourse.nodered.org/t/mosquitto-mqtt-example-without-escaping-quotes/96716/5 "2025-07-22T11:43:14Z")

</div>

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