This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to parse and read JSON file correctly?

Hi guys,

I am using the nRF52832 board and I want to parse and read my JSON file. I don't have too much experience working with cJSON and here you will find my simple code where I try to understand how to define things properly. I will appreciate a lot if you can help me with some advice or better if you can suggest me where I made errors.

Best regards,

Adnan.

/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <sys/printk.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
#include "cJSON.h"


char *create_hello(void) {
	
	char *string;
	
	cJSON *hello = cJSON_CreateObject();
	
	cJSON *functionName = cJSON_CreateString("hello_world");
	cJSON_AddItemToObject(hello, "functionName", functionName);
	
	string = cJSON_Print(hello);
	
	cJSON_Delete(hello);
	return string;
	
	}

void hello_world (void) {
	
	printk("Hello World! %s\n", CONFIG_BOARD);
	}


void main(void)
{
	const char *hello = "{\"functionName\": \"hello_world\"}";
	cJSON *hello_json = cJSON_Parse(hello);
	cJSON *functionName = cJSON_GetObjectItemCaseSensitive(hello_json, "functionName");
	
	while(true){
		
		if (functionName == "hello_world") {
			hello_world();
			k_sleep(K_SECONDS(1));
			}		
			
		}
}

Parents
  • Hello, Aduka!

    Thank you for reaching out. First of all, have you defined the correct prj.conf for your application? Mine looks like this for the hello_world sample:

    # nothing here
    CONFIG_CJSON_LIB=y
    
    # newlibc
    CONFIG_NEWLIB_LIBC=y

    Secondly, your code is mostly correct, but you have done an illegal comparison in your while loop. Look at my main below, where the error is corrected:

    void main(void)
    {
    	const char *hello = "{\"functionName\": \"hello_world\"}";
    	cJSON *hello_json = cJSON_Parse(hello);
    	char* functionName = cJSON_Print(cJSON_GetObjectItemCaseSensitive(hello_json, "functionName"));
    	
    	while(true){
    		
    		if (strcmp(functionName, "hello_world")) {
    			hello_world();
    			k_sleep(K_SECONDS(1));
    			}		
    			
    		}
    }

    In the code above I have changed line 45 so that cJSON_Print is used to write the content to a string. Then I have changed 49 to use strcmp, for comparing the strings. The line numbers I reference are from your original code. 

    Hope this helps you on your way, and please reach out if you wonder about anything else. 

    Best regards,
    Carl Richard

  • Hi Carl,

    Thanks a lot for this help and suggestions! I tried this code with these corrections, but again I am not able to compile it on my board. Now the problem occurs in my proj.conf file I think.

    This is the error:

    error: Aborting due to Kconfig warnings

    CMake Error at /home/adnan/zephyrproject/zephyr/cmake/kconfig.cmake:217 (message):
    command failed with return code: 1
    Call Stack (most recent call first):
    /home/adnan/zephyrproject/zephyr/cmake/app/boilerplate.cmake:510 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:24 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:40 (include_boilerplate)
    CMakeLists.txt:5 (find_package)


    -- Configuring incomplete, errors occurred!
    See also "/home/adnan/zephyrproject/zephyr/build/CMakeFiles/CMakeOutput.log".
    See also "/home/adnan/zephyrproject/zephyr/build/CMakeFiles/CMakeError.log".
    FAILED: build.ninja
    /usr/bin/cmake --regenerate-during-build -S/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson -B/home/adnan/zephyrproject/zephyr/build
    ninja: error: rebuilding 'build.ninja': subcommand failed
    FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/adnan/zephyrproject/zephyr/build

    Can you maybe figure out what could be a potential problem now? And also, I forgot to tell that I use Zephyr if that is important for this problem. Also, my last question is related to Makefile and prj.conf files...where I can find the proper configuration for different samples. For sure, I will need to use cJSON together with my BLE example.

    Thanks in advance and best regards,

    Adnan.

  • Hello again, Adnan!

    I see. Could you provide the whole log? You may have to run the build command with "pristine" to ensure that the new config is applied. Like this:

    west build -b nrf52dk_nrf52832 -p

    ----

    For your second question. You should not need to use Makefiles at all. The two configs above, as well as #include "cJSON.h" should suffice. For BLE samples I suggest looking at what we have in <ncs_root>/nrf/samples/bluetooth/.

    Best regards,
    Carl Richard

  • Hi Carl,

    I tried this and again unsuccessfully. This is the whole message after I tried to compile this sample: 

    west build -b nrf52dk_nrf52832 -p
    -- west build: generating a build system
    Including boilerplate (Zephyr base): /home/adnan/zephyrproject/zephyr/cmake/app/boilerplate.cmake
    -- Application: /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson
    -- Zephyr version: 2.3.0-rc1 (/home/adnan/zephyrproject/zephyr)
    -- Found Python3: /usr/bin/python3.6 (found suitable exact version "3.6.9") found components: Interpreter
    -- Board: nrf52dk_nrf52832
    ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
    -- Found toolchain: zephyr (/home/adnan/zephyr-sdk-0.11.3)
    -- Found west: /home/adnan/.local/bin/west (found suitable version "0.7.2", minimum required is "0.7.1")
    -- Found dtc: /home/adnan/zephyr-sdk-0.11.3/sysroots/x86_64-pokysdk-linux/usr/bin/dtc (found suitable version "1.5.0", minimum required is "1.4.6")
    -- Found BOARD.dts: /home/adnan/zephyrproject/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832.dts
    -- Generated zephyr.dts: /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/build/zephyr/zephyr.dts
    -- Generated devicetree_unfixed.h: /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/build/zephyr/include/generated/devicetree_unfixed.h
    Parsing /home/adnan/zephyrproject/zephyr/Kconfig
    Loaded configuration '/home/adnan/zephyrproject/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832_defconfig'
    Merged configuration '/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/prj.conf'

    /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/prj.conf:2: warning: attempt to assign the value 'y' to the undefined symbol CJSON_LIB

    error: Aborting due to Kconfig warnings

    CMake Error at /home/adnan/zephyrproject/zephyr/cmake/kconfig.cmake:217 (message):
    command failed with return code: 1
    Call Stack (most recent call first):
    /home/adnan/zephyrproject/zephyr/cmake/app/boilerplate.cmake:510 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:24 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:35 (include_boilerplate)
    CMakeLists.txt:5 (find_package)


    -- Configuring incomplete, errors occurred!
    FATAL ERROR: command exited with status 1: /usr/bin/cmake -B/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/build -S/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson -GNinja -DBOARD=nrf52dk_nrf52832

    And also thanks a lot for this advice for BLE example.

    I hope that you will be able to help me again!

    Best regards,

    Adnan.

Reply
  • Hi Carl,

    I tried this and again unsuccessfully. This is the whole message after I tried to compile this sample: 

    west build -b nrf52dk_nrf52832 -p
    -- west build: generating a build system
    Including boilerplate (Zephyr base): /home/adnan/zephyrproject/zephyr/cmake/app/boilerplate.cmake
    -- Application: /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson
    -- Zephyr version: 2.3.0-rc1 (/home/adnan/zephyrproject/zephyr)
    -- Found Python3: /usr/bin/python3.6 (found suitable exact version "3.6.9") found components: Interpreter
    -- Board: nrf52dk_nrf52832
    ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
    -- Found toolchain: zephyr (/home/adnan/zephyr-sdk-0.11.3)
    -- Found west: /home/adnan/.local/bin/west (found suitable version "0.7.2", minimum required is "0.7.1")
    -- Found dtc: /home/adnan/zephyr-sdk-0.11.3/sysroots/x86_64-pokysdk-linux/usr/bin/dtc (found suitable version "1.5.0", minimum required is "1.4.6")
    -- Found BOARD.dts: /home/adnan/zephyrproject/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832.dts
    -- Generated zephyr.dts: /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/build/zephyr/zephyr.dts
    -- Generated devicetree_unfixed.h: /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/build/zephyr/include/generated/devicetree_unfixed.h
    Parsing /home/adnan/zephyrproject/zephyr/Kconfig
    Loaded configuration '/home/adnan/zephyrproject/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832_defconfig'
    Merged configuration '/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/prj.conf'

    /home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/prj.conf:2: warning: attempt to assign the value 'y' to the undefined symbol CJSON_LIB

    error: Aborting due to Kconfig warnings

    CMake Error at /home/adnan/zephyrproject/zephyr/cmake/kconfig.cmake:217 (message):
    command failed with return code: 1
    Call Stack (most recent call first):
    /home/adnan/zephyrproject/zephyr/cmake/app/boilerplate.cmake:510 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:24 (include)
    /home/adnan/zephyrproject/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:35 (include_boilerplate)
    CMakeLists.txt:5 (find_package)


    -- Configuring incomplete, errors occurred!
    FATAL ERROR: command exited with status 1: /usr/bin/cmake -B/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson/build -S/home/adnan/zephyrproject/zephyr/samples/hello_world_cjson -GNinja -DBOARD=nrf52dk_nrf52832

    And also thanks a lot for this advice for BLE example.

    I hope that you will be able to help me again!

    Best regards,

    Adnan.

Children
Related