<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mqtt simple nrf9160, send JSON as utf-8</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/65278/mqtt-simple-nrf9160-send-json-as-utf-8</link><description>Hi, 
 I have configured the MQTT simple sample from nordic to send data to our broker, this works just fine at the moment. 
 
 I would like to send Json formated data to the broker -----&amp;gt; 
 
 { &amp;quot;sensor_id&amp;quot;: &amp;quot;c5278fbf-c6d5-4c48-b51d-652beb609b34&amp;quot;, &amp;quot;value</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 25 Nov 2020 10:20:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/65278/mqtt-simple-nrf9160-send-json-as-utf-8" /><item><title>RE: Mqtt simple nrf9160, send JSON as utf-8</title><link>https://devzone.nordicsemi.com/thread/281749?ContentTypeID=1</link><pubDate>Wed, 25 Nov 2020 10:20:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:60e2e9e9-495d-445c-8d9b-19a3725c4f80</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi again, David!&lt;br /&gt;&lt;br /&gt;If you want to use numbers with the cJSON library, have a look at my &lt;a href="https://devzone.nordicsemi.com/support-private/support/260263#permalink=614901"&gt;answer here&lt;/a&gt;. The important part is to include cJSON_OS.h and call cJSON_Init at the start of main.&lt;br /&gt;Remember that&amp;nbsp;CONFIG_HEAP_MEM_POOL_SIZE and&amp;nbsp;CONFIG_MAIN_STACK_SIZE must be non-zero values.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt simple nrf9160, send JSON as utf-8</title><link>https://devzone.nordicsemi.com/thread/266716?ContentTypeID=1</link><pubDate>Thu, 27 Aug 2020 11:41:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9443f44c-c7f8-415a-a852-e0205c552792</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi again, David!&lt;br /&gt;&lt;br /&gt;I&amp;#39;ve rewritten it to your configuration. However, there is an issue with the cJSON library that prevents it from handling doubles from what I could tell. Therefore you need to convert from string (i.e. &amp;quot;13.0&amp;quot;) to float (i.e. 13.0) on the cloud end.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void transmit()
{
	char *out;
	char *id1 = &amp;quot;c5278fbf-c6d5-4c48-b51d-652beb609b34&amp;quot;;

	char value_buf[6];
	double value = 13.0;

	snprintf(value_buf, sizeof(value_buf), &amp;quot;%.1f&amp;quot;,
	value);

	printk(&amp;quot;Value: %s\n&amp;quot;, value_buf);

	cJSON *root;

	/* create root node and array */
	root = cJSON_CreateObject();	

	/* add sensor data */
	cJSON_AddItemToObject(root, &amp;quot;sensor_id&amp;quot;, cJSON_CreateString(id1));
	cJSON_AddItemToObject(root, &amp;quot;value&amp;quot;, cJSON_CreateString(value_buf));

	/* print everything */
	out = cJSON_Print(root);
	if (out == NULL) {
		printk(&amp;quot;Failed print...\n&amp;quot;);
	}
    printk(&amp;quot;%s\n&amp;quot;, out);

	/* Publish with MQTT */
	data_publish(&amp;amp;client, MQTT_QOS_1_AT_LEAST_ONCE,
   			out, strlen(out));

	free(out);

	/* free all objects under root and root itself */
	cJSON_Delete(root);

	return;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;I hope this works out!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt simple nrf9160, send JSON as utf-8</title><link>https://devzone.nordicsemi.com/thread/266675?ContentTypeID=1</link><pubDate>Thu, 27 Aug 2020 09:26:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:382f7ae4-b228-4ab5-9b06-c6d5a6272997</guid><dc:creator>David</dc:creator><description>&lt;p&gt;I want it like this ---&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;br /&gt; &amp;quot;sensor_id&amp;quot;: &amp;quot;c5278fbf-c6d5-4c48-b51d-652beb609b34&amp;quot;,&lt;br /&gt; &amp;quot;value&amp;quot;: &amp;quot;13.0&amp;quot;&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;without the quotes around 13.0&lt;/p&gt;
&lt;p&gt;David&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt simple nrf9160, send JSON as utf-8</title><link>https://devzone.nordicsemi.com/thread/266672?ContentTypeID=1</link><pubDate>Thu, 27 Aug 2020 09:24:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b41db87c-05ff-4288-b31a-39577ab97e58</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;You do not want the &amp;quot;sensors&amp;quot; array on top. Do interpret it correctly?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt simple nrf9160, send JSON as utf-8</title><link>https://devzone.nordicsemi.com/thread/266657?ContentTypeID=1</link><pubDate>Thu, 27 Aug 2020 08:59:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4133ede1-b29e-4dfc-86c8-66005d5bc186</guid><dc:creator>David</dc:creator><description>&lt;p&gt;I managed to adapt the example, so now I only need to remove the quotes &amp;quot;&amp;quot; from the value1 &amp;quot;13.0&amp;quot;.&lt;/p&gt;
&lt;p&gt;Do you have an idea how to do this?&lt;/p&gt;
&lt;p&gt;David&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt simple nrf9160, send JSON as utf-8</title><link>https://devzone.nordicsemi.com/thread/266647?ContentTypeID=1</link><pubDate>Thu, 27 Aug 2020 08:34:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f0bd500e-816e-41e8-8d5c-b63b16746cd1</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hello, David!&lt;br /&gt;&lt;br /&gt;I managed to publish and receive jSON data&amp;nbsp;using the following code:&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;static void transmit() {
   char *out;
   char *id1 = &amp;quot;c5278fbf-c6d5-4c48-b51d-652beb609b34&amp;quot;; 
   char *id2 = &amp;quot;c5278fgf-c6d5-4c48-b51d-652beb6s9b34&amp;quot;; 
   char *value1 = &amp;quot;13.0&amp;quot;;
   char *value2 = &amp;quot;117.0&amp;quot;;
   cJSON *root, *sensors, *sensor;

   /* create root node and array */
   root = cJSON_CreateObject();
   sensors = cJSON_CreateArray();

   /* add sensors array to root */
   cJSON_AddItemToObject(root, &amp;quot;sensors&amp;quot;, sensors);

   /* add 1st sensor to sensors array */
   cJSON_AddItemToArray(sensors, sensor = cJSON_CreateObject());
   cJSON_AddItemToObject(sensor, &amp;quot;value&amp;quot;, cJSON_CreateString(value1));
   cJSON_AddItemToObject(sensor, &amp;quot;sensor_id&amp;quot;, cJSON_CreateString(id1));

   /* add 1st sensor to sensors array */
   cJSON_AddItemToArray(sensors, sensor = cJSON_CreateObject());
   cJSON_AddItemToObject(sensor, &amp;quot;value&amp;quot;, cJSON_CreateString(value2));
   cJSON_AddItemToObject(sensor, &amp;quot;sensor_id&amp;quot;, cJSON_CreateString(id2));

   /* print everything */
   out = cJSON_Print(root);
   printk(&amp;quot;%s\n&amp;quot;, out);

   /* Publish with MQTT */
   data_publish(&amp;amp;client, MQTT_QOS_1_AT_LEAST_ONCE,
				out, strlen(out));

   free(out);

   /* free all objects under root and root itself */
   cJSON_Delete(root);

   return;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Could you try to adapt this to your use?&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Edit: code is based on the following&amp;nbsp;&lt;a href="https://stackoverflow.com/questions/45709054/create-json-object-using-cjson-h"&gt;stack overflow ticket&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>