# Output a very simple JSON object instead of a string?

**URL:** <https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414>\
**Category:** General\
**Created:** [9 January 2024 10:37 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414 "2024-01-09T10:37:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 January 2024 10:37 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/1 "2024-01-09T10:37:41Z")

</div>

Hi,

I have looked at the ArdiunoJSON library, but it is very complicated and I only need something very simple.

At the moment I send out a **string** of data. Example of my code ...  
`MQTT_client.publish ( Module 99 /info/data , "22 - Current temperature = 8.9");`

What I would like to have is a **JSON pair** of ...  
`MQTT_client.publish (Module 99 /info/data, {"22 - Current temperature =":8.9})`

As you can see my requirements are very simple, but I do not know how to do it ?

I have asked this question on the Arduino forum with some success, this is the code from their ..

```auto
char json[80];

      Current_temperature = 21.345;
      Convert_temperature_to_2_decimal_places ();                                          
      dtostrf(Temperature_to_2_decimal_places, 4, 1, Buffer);                               

      strcpy(json, "{\"22 - Current temperature =\":");
      strcat(json, Buffer);
      strcat(json, "}");

      Serial.print( "Current_temperature = ");
      Serial.println(Current_temperature);
      Serial.print("json = ");
      Serial.println(json);

      Print_MQTT_temperature_sent ();
      MQTT_client.publish (Topic_02, json);

```

I have spent many more hours om my own trying to find what I did wrong.But when this is sent out via MQTT, I receive in Node-RED one single object, so I cannot separate the value ?

The value I receive is {"22 - Current temperature =":21.3}

The object is ...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b73e0fe113b1dbe04c5f5567924b666a9321931d.png)

Thanks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 January 2024 10:58 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/2 "2024-01-09T10:58:37Z")

</div>

> [@questuk](#):
>
> I receive in Node-RED one single object, so I cannot separate the value

what do you mean? Since it is an object, you can simply access the value using `msg.payload.name_of_property`

However, since you have chosen a god awful property name and awful topic structure, you are just making things difficult for yourself!

Recommendation:

1. structure your topics like `module/99/info/blahblah`
2. name properties in a way that doesnt require square bracket notation to access them. e.g. `current_temperature` instead of `22 - Current temperature =`

You will eventually realise the benefit of a good topic structure but until that moment of realisation, please take on board what we have suggested here and previously:

> [@How can I receive & send Data in Node-RED as JSON ... in a easy to understand way?](https://discourse.nodered.org/t/how-can-i-receive-send-data-in-node-red-as-json-in-a-easy-to-understand-way/84242/10):
>
> PS:
> 
> a better topic structure would be `module/22/xxxxx` this way you can subscribe to all modules `module/#` or a single module `module/22/#` or a specif topic of all modules `module/+/temperature`

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 January 2024 11:37 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/3 "2024-01-09T11:37:04Z")

</div>

Hi Thanks for that Steve

I must admit I have been looking at this until late last night and I think I have confused myself !

Sometimes it takes someone to say that my JSON object is at least correct ( yes my format Topic is terrible ) but this is from when it was just a string sent out by my Arduino, so it didn't matter too much.

So I am now trying to sort out my Arduino code which was written about 4 years ago, before I had even heard of JSON.  
😳

Thanks

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 January 2024 15:19 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/4 "2024-01-09T15:19:31Z")

</div>

Hi Again,

I need more help, again I have tried for the last hour and I think I am a little closer.

I want temperature and humidity both to be in a single JSON Object.  
Example ... **{"temperature":21.3,"humidity":46.2}**

I seem to have too many inverted commas ? and missing one comma after 21.3  
My code so far produces a string `"{"temperature":21.3"humidity":46.2}"`

Any help would be great 👍

```
  char json[80];                                                                        

  float temperature = 21.345;                                      
  dtostrf(temperature, 4, 1, Buffer);   

  float humidity = 46.222;
  dtostrf(humidity, 4, 1, Buffer1); 

  strcpy(json, "{\"temperature\":");
  strcat(json, Buffer);

  strcat(json, "\"humidity\":");
  strcat(json, Buffer1);

  strcat(json, "}");

  MQTT_client.publish (Topic_02, json);

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 January 2024 15:41 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/5 "2024-01-09T15:41:30Z")

</div>

I dont see any issue.

PS: [arduinojson](https://arduinojson.org/) makes this MUCH easier

```auto
JsonDocument doc;
doc["temperature"] = temperature;
doc["humidity"] = humidity;
serializeJson(doc, json)

```

---

<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:** [9 January 2024 15:42 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/6 "2024-01-09T15:42:14Z")

</div>

Set the mqtt node to auto detect json

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 January 2024 16:05 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/7 "2024-01-09T16:05:59Z")

</div>

Colin I didn't know it could do that, although I am using a very old version and as I have about 15 ESP modules all around my house, I thought I would change as little as possible for now, then when I test in one unit and get it to work, then look to change all of them to the new code when I understand things better.

Steve, At the moment I am trying to avoid the ArduinoJSON because my modules are very time critical as they are constantly looping with as little delay as possible. But I may go to ArduinoJSON when i need to test it.

Example ... **{"temperature":21.3,"humidity":46.2}**  
But for the moment if I can, I would like to this like the above as I know that will have no impact on timing.

I want to do this in Node-RED debug, _( image example got from internet )._  
 ![10](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0ff70b93b5d05f44137719b40f7b6d72feac8142.png)

Here are the problems with my output,  
 ![problem](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0b696fef0ce0fe2474aba97db5d060a76f2b382.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 January 2024 16:10 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/8 "2024-01-09T16:10:13Z")

</div>

> [@questuk](#):
>
> Here are the problems with my output,  
> ![problem](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0b696fef0ce0fe2474aba97db5d060a76f2b382.png)

the outer quotes are denoting this is a string (they are added in the debug view)  
you are missing a comma only.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 January 2024 16:12 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/9 "2024-01-09T16:12:02Z")

</div>

> [@questuk](#):
>
> Steve, At the moment I am trying to avoid the ArduinoJSON because my modules are very time critical as they are constantly looping with as little delay as possible. But I may go to ArduinoJSON when i need to test it.

I highly doubt the lib will be MUCH less efficient than the hand coded string parsing you are doing? But only a test would reveal that.

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 January 2024 16:31 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/10 "2024-01-09T16:31:25Z")

</div>

Thanks Steve,

Added the comma and voila all working. 😀

I have learnt more doing it this way as I have never used **strcpy** or **strcat** before.

One question i know that a ` \` is an escape but how is it being used here, as there are so many inverted _commas_ and _colon's_, i get lost exactly what it is all doing ?

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [9 January 2024 16:37 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/11 "2024-01-09T16:37:44Z")

</div>

And for anyone that may read this in the future, I will add the finished code below.  
As I hate it when you follow a subject and they find a solution but they forget to post the final code. This includes the missing comma.

```
  char json[80];            

  float temperature = 21.345;                                      
  dtostrf(temperature, 4, 1, Buffer);   

  float humidity = 46.222;
  dtostrf(humidity, 4, 1, Buffer1); 

  strcpy(json, "{\"temperature\":");
  strcat(json, Buffer);

  strcat(json, ",\"humidity\":");
  strcat(json, Buffer1);

  strcat(json, "}");

  MQTT_client.publish (Topic_02, json);

```

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [13 January 2024 14:57 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/12 "2024-01-13T14:57:50Z")

</div>

Hi again,

I feel a little stupid but I cannot get this to work 🙄  
It should be shown in Node-RED as a key value pair object.  
What I am getting is` "{"PIR":movement}"` in my debug node.

I have tried everything I can think of and looked on the internet for an hour.

I think it is something very simple but I cannot find it !

My code

```auto
 char MQTT_Text_to_send[] = "movement";

      strcpy (json_data, "{\"PIR\":"); // Copy this as it is the start of a key-value pair.
      strcat (json_data, MQTT_Text_to_send); // Append to json_data, this Joins 2 Char arrays ?
      strcat (json_data, "}"); // Required json_data output.
        
      Serial.println ("");
      Serial.print ("PIR = ");    
      Serial.println (json_data);

      MQTT_client.publish (Topic_01, json_data);      

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 January 2024 15:14 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/13 "2024-01-13T15:14:43Z")

</div>

The `"movement"` needs to be quoted, so the JSON string is valid.  
try

```auto
char MQTT_Text_to_send[] = "movement";

      strcpy (json_data, "{\"PIR\": \""); // Copy this as it is the start of a key-value pair.
      strcat (json_data, MQTT_Text_to_send); // Append to json_data, this Joins 2 Char arrays ?
      strcat (json_data, "\"}"); // Required json_data output.
        
      Serial.println ("");
      Serial.print ("PIR = ");    
      Serial.println (json_data);

      MQTT_client.publish (Topic_01, json_data);

```

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [13 January 2024 15:24 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/14 "2024-01-13T15:24:05Z")

</div>

Thanks E1cid,

That worked, 1 hour wasted and I knew if I came on here someone would see my mistake !  
But I do try and find the problem without just coming here and asking.

The inverted commas drive me mad 🙄

Thanks

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 January 2024 15:27 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/15 "2024-01-13T15:27:55Z")

</div>

> [@questuk](#):
>
> The inverted commas drive me mad 🙄

ahem cough cough 😉

> [@Steve-Mcl](#):
>
> PS: [arduinojson](https://arduinojson.org/) makes this MUCH easier
> 
> ```auto
> JsonDocument doc;
> doc["temperature"] = temperature;
> doc["humidity"] = humidity;
> serializeJson(doc, json)
> 
> ```

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [13 January 2024 15:44 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/16 "2024-01-13T15:44:50Z")

</div>

HI Steve

Ha ha I knew you would say that, i have tried to use it, but it would not work on my code.  
🙄

I seem to remember it was throwing errors and I cannot update my Arduino board manager past 2.7.4 at the moment, as again I get errors with my code & Arduino libraries.

My code was added to over a 4 year period with various libraries added, of which most are out of date now. All rolled into one master universal code that can do everything i need for my home automation.  
There are about 12 modules in my home all doing different things and reporting back to Node-RED. So it is complicated.

I will have another go soon at ArduinoJSON  
Mistakingly I thought the simple way would be quicker 🙃

Thanks for all your help men. 🙂

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [13 January 2024 17:40 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/17 "2024-01-13T17:40:38Z")

</div>

Hi,

Hopefully the last time I will have to ask for help on this subject,

```auto
      strcpy (json_data, "{\"PIR-activated\": \""); 
      strcat (json_data, "true"); 
      strcat (json_data, "\"}");

```

This works fine, and true is received as text, is there away of sending a **boolean**  **true**?

Thanks again 👍

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 January 2024 18:03 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/18 "2024-01-13T18:03:02Z")

</div>

`true` in JSON syntax would be with no quotes

```auto
strcpy (json_data, "{\"PIR-activated\": "); 
      strcat (json_data, "true"); 
      strcat (json_data, "}");

```

e.g

```auto
{"PIR-activated": true}

```

> **[What Is JSON and How Do You Use It?](https://www.howtogeek.com/devops/what-is-json-and-how-do-you-use-it/)**
>
> JSON (JavaScript Object Notation) is a standardized format for representing structured data.

> **[A beginner's guide to JSON, the data format for the internet - Stack Overflow](https://stackoverflow.blog/2022/06/02/a-beginners-guide-to-json-the-data-format-for-the-internet/)**

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [13 January 2024 18:40 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/19 "2024-01-13T18:40:50Z")

</div>

Thanks,

I thought I had tried that combination, obviously not !

Worked 👍

Thanks again

---

<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:** [13 March 2024 18:41 UTC](https://discourse.nodered.org/t/output-a-very-simple-json-object-instead-of-a-string/84414/20 "2024-03-13T18:41:06Z")

</div>

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