<?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>cJSON error</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/68664/cjson-error</link><description>Dear Sir. 
 
 I am using the cJSON driver . 
 When it comes to string it works fine . 
 However when I use integer and swap the lines 
 
 //cJSON_AddStringToObject(root, &amp;quot;payload&amp;quot;, payload); cJSON_AddNumberToObject(root, &amp;quot;payload&amp;quot;, 1243); 
 
 I can use</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 03 Mar 2022 18:29:58 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/68664/cjson-error" /><item><title>RE: cJSON error</title><link>https://devzone.nordicsemi.com/thread/356141?ContentTypeID=1</link><pubDate>Thu, 03 Mar 2022 18:29:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e5a66f4b-e6f7-48ae-ad25-bf74f7f7f8a7</guid><dc:creator>Thabet</dc:creator><description>&lt;p&gt;Hello&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/members/crfosse"&gt;Carl Richard&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I know this topic is old, but I have this problem and apparently this solution is no longer working, could you give another solution to work with numbers through the cJSON lib?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I have no problem using cJSON_AddItemToObject, I can send strings, but when I try to send any number I find the problem. I have also configured: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;CONFIG_MAIN_STACK_SIZE=16384 &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;CONFIG_HEAP_MEM_POOL_SIZE=16384&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I inserted all the recommendations both in prj.conf, and in relation to libraries and cJSON_Init at the beginning of main, but I keep getting a Hard Fault, could you check if this solution still works for you? I&amp;#39;m using NCS 1.8.0!&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cJSON error</title><link>https://devzone.nordicsemi.com/thread/281747?ContentTypeID=1</link><pubDate>Wed, 25 Nov 2020 10:17:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b477290-f045-4c4d-bacb-d8df82b208d8</guid><dc:creator>Carl Richard</dc:creator><description>&lt;p&gt;Hi, Ephraim!&lt;br /&gt;&lt;br /&gt;Good that you are progressing. Some comments:&lt;br /&gt;- One line 22 of your code you are adding a number object to the array in the wrong way, which in itself may cause trouble. &lt;br /&gt;- On line 27 you are creating an entirely new array. If you want to add the existing array use cJSON_AddItemToObject(root, &amp;quot;payload&amp;quot;, payload).&lt;br /&gt;&lt;br /&gt;Anyway, I got this working with the code below:&amp;nbsp;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;
#include &amp;lt;cJSON.h&amp;gt;
#include &amp;lt;cJSON_os.h&amp;gt;

void createJsonFromParams(void)
{
	char *command = &amp;quot;periodic&amp;quot;; //  &amp;quot;holter&amp;quot;  &amp;quot;holtersm&amp;quot; &amp;quot;spot&amp;quot;   &amp;quot;time&amp;quot;
	char *type = &amp;quot;8&amp;quot;;

	cJSON *root = cJSON_CreateObject();
	cJSON_AddStringToObject(root, &amp;quot;command&amp;quot;, command);
	cJSON_AddStringToObject(root, &amp;quot;type&amp;quot;, type);
   //cJSON_AddNumberToObject(root, &amp;quot;number&amp;quot;, 33);

   cJSON * payload = cJSON_AddArrayToObject(root, &amp;quot;payload&amp;quot;);

   cJSON_AddItemToArray(payload, cJSON_CreateNumber(33));

   printk(&amp;quot;%s&amp;quot;,cJSON_Print(root));
   cJSON_Delete(root);
}

void main(void)
{
   cJSON_Init();

	while (1) {
      createJsonFromParams();
		k_sleep(K_MSEC(5000));
	}
}&lt;/pre&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And the following prj.conf:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;CONFIG_SERIAL=y
CONFIG_DK_LIBRARY=y
CONFIG_CJSON_LIB=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_ASSERT=y

CONFIG_HEAP_MEM_POOL_SIZE=8192
CONFIG_MAIN_STACK_SIZE=8192&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Note that cJSON_Init is called at the start of main. This enables use and printing of the number object.&lt;br /&gt;&lt;br /&gt;Best regads,&lt;br /&gt;Carl Richard&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cJSON error</title><link>https://devzone.nordicsemi.com/thread/281706?ContentTypeID=1</link><pubDate>Wed, 25 Nov 2020 07:23:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f56d2583-f02b-4432-8870-0bd7f794af4e</guid><dc:creator>ephraim</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;This problem is over .&lt;/p&gt;
&lt;p&gt;I added the following lines to prj.conf .&lt;/p&gt;
&lt;p&gt;CONFIG_CJSON_LIB=y&lt;br /&gt;CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However a new problem that I encounter,&lt;/p&gt;
&lt;p&gt;I do able to produce a JSON string .&lt;/p&gt;
&lt;p&gt;In the string I insert a array , I can&amp;#39;t fill the array with data like [12,13,45,234,34,5,99]&lt;/p&gt;
&lt;p&gt;The array is always empty [] ,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I use the following code.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;.&lt;pre class="ui-code" data-mode="text"&gt;cJSON * createJsonFromParams(void)
{

  char * command = &amp;quot;periodic&amp;quot; ;//  &amp;quot;holter&amp;quot;  &amp;quot;holtersm&amp;quot; &amp;quot;spot&amp;quot;   &amp;quot;time&amp;quot;
  char * type  = &amp;quot;8&amp;quot;;
//  char * start_time  = &amp;quot;1845&amp;quot;;
//  char * duration  = &amp;quot;104&amp;quot;;
//  char * rest  = &amp;quot;29&amp;quot;;
//  char * stop_time  = &amp;quot;27000&amp;quot;;
//  char * payload =  &amp;quot;Etrog2020&amp;quot;;
//  int * payload = 123456; //&amp;quot;Etrog2020&amp;quot;; //  {0x06,0x05,0x04,0x03,0x02,0x01}; //
//  cJSON *number_item = cJSON_CreateNumber(1942);

  cJSON *root = cJSON_CreateObject();
  cJSON_AddStringToObject(root, &amp;quot;command&amp;quot;, command);
  cJSON_AddStringToObject(root, &amp;quot;type&amp;quot;, type);

 
   cJSON * payload = cJSON_CreateArray();

 
   cJSON_AddItemToObject(root,payload, cJSON_CreateNumber(129));
    cJSON_AddItemToArray(payload, cJSON_CreateNumber(33));


    // Write the array &amp;quot;payload&amp;quot; to stream.
    cJSON_AddArrayToObject(root, &amp;quot;payload&amp;quot;);

 

  return root;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This is the JSON Iget&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/3716.json.png" /&gt;&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cJSON error</title><link>https://devzone.nordicsemi.com/thread/281702?ContentTypeID=1</link><pubDate>Wed, 25 Nov 2020 07:10:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cbbe2231-1c12-4a97-9895-3eded09406b9</guid><dc:creator>ephraim</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have managed to insert&amp;nbsp;&amp;nbsp;cJSON_AddNumberToObject .&lt;/p&gt;
&lt;p&gt;However a new error&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>