Convert JSON into string for MQTT I am trying to publish json object into string with following snippet 

JSONVar data;
data["chipid"] = chipId;
data["co2_equivalent"] = co2_equivalent.c_str();
data["tvoc"] = tvoc.c_str();
data["h2_signal"] = h2_signal;
data["ethanol_signal"] = ethanol_signal;
data["humidity"] = humidity;
data["temperature"] = temperature;
data["absolute_hum"] = v.absolute_hum;
data["co2_equivalent_baseline"] = co2_equivalent_baseline;
data["tvoc_baseline"] = tvoc_baseline;

String data_json = JSON.stringify(data);
Serial.print(data_json);
Serial.println();
client.publish("/devices/data", data_json);


This give me following error during compilation.

  /Users/ubuntu/Desktop/Arduino/data_collection/esp32-svm30-mqtt.ino/esp32-svm30-mqtt.ino.ino: In function 'void loop()':
  esp32-svm30-mqtt.ino:271:46: error: no matching function for call to 'PubSubClient::publish(const char [14], String&amp;)'
       client.publish("/devices/data", data_json);
                                                ^
  In file included from /Users/ubuntu/Desktop/Arduino/data_collection/esp32-svm30-mqtt.ino/esp32-svm30-mqtt.ino.ino:2:0:
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:141:12: note: candidate: boolean PubSubClient::publish(const char*, const char*)
      boolean publish(const char* topic, const char* payload);
              ^
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:141:12: note:   no known conversion for argument 2 from 'String' to 'const char*'
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:142:12: note: candidate: boolean PubSubClient::publish(const char*, const char*, boolean)
      boolean publish(const char* topic, const char* payload, boolean retained);
              ^
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:142:12: note:   candidate expects 3 arguments, 2 provided
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:143:12: note: candidate: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
      boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
              ^
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:143:12: note:   candidate expects 3 arguments, 2 provided
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:144:12: note: candidate: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
      boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
              ^
  /Users/ubuntu/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:144:12: note:   candidate expects 4 arguments, 2 provided
  Multiple libraries were found for "PubSubClient.h"
   Used: /Users/ubuntu/Documents/Arduino/libraries/PubSubClient
  Multiple libraries were found for "Arduino_JSON.h"
   Used: /Users/ubuntu/Documents/Arduino/libraries/Arduino_JSON
  Multiple libraries were found for "svm30.h"
   Used: /Users/ubuntu/Documents/Arduino/libraries/svm30
  Multiple libraries were found for "Wire.h"
   Used: /Users/ubuntu/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/Wire
  Multiple libraries were found for "WiFi.h"
   Used: /Users/ubuntu/Library/Arduino15/packages/esp32/hardware/esp32/1.0.4/libraries/WiFi
   Not used: /Applications/Arduino.app/Contents/Java/libraries/WiFi
  exit status 1
  no matching function for call to 'PubSubClient::publish(const char [14], String&amp;)'


Line Serial.print(data_json) does print it in serial monitor. 

Update : 

If I doclient.publish("/devices/data", data_json.c_str());, it gets compiled, but data doesn't appear in mqtt.

Serial.print(data_json.length()); prints 236

Is my json to string conversion correct ?
Please note: the question originally had no mention of trying to use c_str() - so I'm not a retard


Point 1 JSON actually is a string
Point 2 your data_json varibale is a String
Point 3 client.publish doesn't work take String as the payload argument, it takes either const char * or const uint8_t *

So, what you can simply do is use Strings c_str method to pas a const char * as the payload

i.e.

client.publish("/devices/data", data_json.c_str());


Point 4 - if you read the pubsubclient documentation, you'll see that 


  The maximum message size, including header, is 128 bytes by default. This is configurable via MQTT_MAX_PACKET_SIZE in PubSubClient.h. Longer messages can also be sent with the publish_P() method.


so what you may be experiencing now is a message size too big for pubsubclient to handle - set a larger packet size before including the PubSubClientHeader.h

#define MQTT_MAX_PACKET_SIZE 1024
#include &lt;pubsubclient.h&gt;


If that also doesn't work, try

client.publish_P("/devices/data", data_json.c_str());