# Error, Json string ends unexpectedly

**URL:** https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726
**Category:** General
**Tags:** node-red-dashboard, debugging, mqtt
**Created:** [24 April 2022 16:01 UTC](https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726 "2022-04-24T16:01:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![king877](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@king877](https://discourse.nodered.org/u/king877)
#### Post date: [24 April 2022 16:01 UTC](https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726/1 "2022-04-24T16:01:16Z")

</div>

So I am trying to publish some values to a topic using jsondoc, but unfortunately only some of the values get published. It sends 6 out of the 10. My memory is 256 for the doc here is the code. The output I get in node red is

`{"hat_temperature":31.3,"hat_humidity":27.55,"hat_pressure":987.87,"Sen_temperature":null,"sen_humidity":null,"sen_brightness":1`

I have an MQTT in node connected to debug node.

```auto
StaticJsonDocument<256> doc;

    doc["hat_temperature"] = 31.3;
    doc["hat_humidity"] = 27.55;
    doc["hat_pressure"] = 987.87;
    doc["Sen_temperature"] = dht.readTemperature();
    doc["sen_humidity"] = dht.readHumidity();
    doc["sen_brightness"] = 12;
    doc["sen_lights"] = "on";
    doc["sen_presence"] = "present";
    doc["ID"] = "HAMK_876";
    doc["message"] = "Test";
    
     serializeJson(doc, obj);
     Serial.print(obj);
 mqttClient.beginMessage(topic);
    mqttClient.print(obj);
    mqttClient.endMessage();

```

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [24 April 2022 17:02 UTC](https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726/2 "2022-04-24T17:02:00Z")

</div>

This is what my Arduino publish routine looks like. I had to increase the StaticJsonDocument size to 384 otherwise nothing got publish. It took a long time tracking down the problem. Hope this helps.

```auto
void publish_the_values()
{
  if (debug) {
    Serial.print("Celsius temperature: ");
    Serial.print(ds18b20_temp_C);
    Serial.print(" - Fahrenheit temperature: ");
    Serial.println(ds18b20_temp_F);
  }
  
  StaticJsonDocument<384> doc;
  doc["report"] = "readings";
  doc["loc"] = location;
  doc["reason"] = reason;
  doc["batt"] = battery_voltage;
  doc["bb_C"] = ds18b20_temp_C;
  doc["bb_F"] = ds18b20_temp_F;
  doc["cf_t"] = cf_temperature;
  doc["cf_h"] = cf_humidity;
  doc["cf_p"] = cf_pressure;
  doc["ssid"] = WiFi.SSID();
  doc["rssi"] = WiFi.RSSI();
  doc["ip"] = WiFi.localIP();

  char buffer[384];
  size_t n = serializeJson(doc, buffer);

  char topic[40]; // 40 chars wide
  strcpy(topic, mqtt_publish_topic);
  strcat(topic, "/");
  strcat(topic, location);

  if (debug) {
    Serial.print("Publish topic: ");
    Serial.println(topic);
    Serial.println(buffer);
  }
  
  mqtt_client.publish(topic, buffer, n);
} 
//------------ End of publish_the_values()

```

---

<div class="post-metadata">

### Author: ![king877](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@king877](https://discourse.nodered.org/u/king877)
#### Post date: [25 April 2022 03:27 UTC](https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726/3 "2022-04-25T03:27:25Z")

</div>

What did you use for calculating the value of the document?

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [25 April 2022 06:24 UTC](https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726/4 "2022-04-25T06:24:19Z")

</div>

In my situation nothing was being sent via MQTT, so I just increased the size from 256 to 384 (trial and error). I did try visually counting the characters in the message but gave up.  
I'm using version 6 of the ArduinoJson library by Benoit Blanchon. There is an Assistant program on his website that enables an accurate length of the document-string to be calculated.

> **[Assistant](https://arduinojson.org/v6/assistant/)**
>
> The ArduinoJson Assistant is an online tool that computes the required JsonDocument capacity for a given document and generates a sample program.

> **[The New ArduinoJson Assistant](https://arduinojson.org/news/2021/02/17/new-arduinojson-assistant/)**
>
> Over the past four months, the Assistant went through a slow and progressive transformation. Let’s see what it offers today.

---

<div class="post-metadata">

### Author: ![king877](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@king877](https://discourse.nodered.org/u/king877)
#### Post date: [25 April 2022 08:43 UTC](https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726/5 "2022-04-25T08:43:07Z")

</div>

ok thanks, for your help. Realized my prob was that i used too long value names. Your value names are short so that helped me to realize my issue.

---

<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: [24 June 2022 08:43 UTC](https://discourse.nodered.org/t/error-json-string-ends-unexpectedly/61726/6 "2022-06-24T08:43:35Z")

</div>

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