Search code examples
cjsonjson-c

Valid json check in C language


I have searched a lot in Google. I want to programmatically check if the string below is valid JSON in C. How can I do that? (I am currently using the json-c library.)

        char * string = "{
                      number1 : 100,
                    number2 : 10,
                    files : [ c , c++, java,PHP,java,PHP ],
                    random: [123567876523333,908,988]
                    }";

The library has no function to check if the string is valid JSON.


Solution

  • You can use the function json_tokener_parse(const char *str), it returns NULL if parsing fails.

    See here for details.