Using JSON with nRF5340 and nRF Connect

Hi

Are there any examples of how to use JSON with nRF5340 DK (or similar) and nRF Connect?

I am using SDK v2.4.1.

I found these files:

c:\ncs\v2.4.1\nrf\ext\iperf3\cjson.c and cjson.h

I wasn't able to include them, so I just copied them to my project folder.\I had to comment out the " iperf_config.h" include in  cjson.h.

I was able to write some basic code:

	cJSON *json_obj;
	cJSON *json_item;
	bool b_result;
	const char *key_str = "key_1";
	char *val_str = "value_1";
	char *json_str = NULL;


	// Create empty JSON object
	json_obj = cJSON_CreateObject();
	// Create a JSON item
	json_item = cJSON_CreateString(val_str);
	// Add an item to this object
	b_result = cJSON_AddItemToObject(json_obj, key_str, json_item);
	// Print the full string
	json_str = cJSON_Print(json_obj);
	// Finished with object, so delete it!
	cJSON_Delete(json_obj);

Initially I got some error about CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE being 0, so in the prj.conf file I set:

CONFIG_MINIMAL_LIBC_MALLOC=y
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=1000

This builds if I comment out the call to cJSON_Print().

I can continue to work with this - although I don't understand how I would eventually "release" the json_str created by cJSON_Print() etc.

So I was hoping there was sample code out there, with the correct includes etc.

Parents
  • FYI - I mocked up quick samples to show how I parse JSON and create the JSON strings. I didn't test these, but it should get you started.

    Create JSON String

    static bool sample_create_json(void)
    {
        // 1 - Create the root object
        cJSON *root = cJSON_CreateObject();
    
        if (!root)
        {
            printk("Unable to create root CJSON object.\n");
            return false;
        }
    
        // 2 - Add items
        cJSON *numberItem = cJSON_AddNumberToObjectCS(root, "name", 123);
    
        if (!numberItem)
        {
            printk("Unable to create JSON object.\n");
            cJSON_Delete(root);
            return false;
        }
    
        // Add other values as needed
    
        // 3 - Create the JSON string
        char *json = cJSON_PrintUnformatted(root);
    
        if (!json)
        {
            printk("Unable to create JSON string.\n");
            cJSON_Delete(root);
            return false;
        }
    
        // 4 - Do something with the JSON that was created
    
        // 5 - Always free the memory that was allocated.
        cJSON_Delete(root);
        return true;
    }
    

    Parse JSON String

    static bool sample_parse_json(const char *json)
    {
        __ASSERT_NO_MSG(json);
    
        // 1 - Parse the JSON
        cJSON *root = cJSON_Parse(json);
    
        // 2 - Verify it was parsed correctly
        if (!root)
        {
            const char *error = cJSON_GetErrorPtr();
    
            if (error)
            {
                printk("JSON Parse Error: %s\n", error);
            }
    
            return false;
        }
    
        // 3 - Do something with the parsed data
        for (cJSON *item = root; item; item = item->next)
        {
            const cJSON *child = item->child;
    
            while (child)
            {
                if (cJSON_IsNumber(child))
                {
                    // Process number
                }
                else if (cJSON_IsString(child))
                {
                    // Process string
                }
                else if (cJSON_IsArray(child))
                {
                    // Process array
                }
                else
                {
                    // Etc...
                }
    
                child = child->next;
            }
        }
    
        // 5 - Always free the memory that was allocated.
        cJSON_Delete(root);
        return true;
    }

Reply
  • FYI - I mocked up quick samples to show how I parse JSON and create the JSON strings. I didn't test these, but it should get you started.

    Create JSON String

    static bool sample_create_json(void)
    {
        // 1 - Create the root object
        cJSON *root = cJSON_CreateObject();
    
        if (!root)
        {
            printk("Unable to create root CJSON object.\n");
            return false;
        }
    
        // 2 - Add items
        cJSON *numberItem = cJSON_AddNumberToObjectCS(root, "name", 123);
    
        if (!numberItem)
        {
            printk("Unable to create JSON object.\n");
            cJSON_Delete(root);
            return false;
        }
    
        // Add other values as needed
    
        // 3 - Create the JSON string
        char *json = cJSON_PrintUnformatted(root);
    
        if (!json)
        {
            printk("Unable to create JSON string.\n");
            cJSON_Delete(root);
            return false;
        }
    
        // 4 - Do something with the JSON that was created
    
        // 5 - Always free the memory that was allocated.
        cJSON_Delete(root);
        return true;
    }
    

    Parse JSON String

    static bool sample_parse_json(const char *json)
    {
        __ASSERT_NO_MSG(json);
    
        // 1 - Parse the JSON
        cJSON *root = cJSON_Parse(json);
    
        // 2 - Verify it was parsed correctly
        if (!root)
        {
            const char *error = cJSON_GetErrorPtr();
    
            if (error)
            {
                printk("JSON Parse Error: %s\n", error);
            }
    
            return false;
        }
    
        // 3 - Do something with the parsed data
        for (cJSON *item = root; item; item = item->next)
        {
            const cJSON *child = item->child;
    
            while (child)
            {
                if (cJSON_IsNumber(child))
                {
                    // Process number
                }
                else if (cJSON_IsString(child))
                {
                    // Process string
                }
                else if (cJSON_IsArray(child))
                {
                    // Process array
                }
                else
                {
                    // Etc...
                }
    
                child = child->next;
            }
        }
    
        // 5 - Always free the memory that was allocated.
        cJSON_Delete(root);
        return true;
    }

Children
Related