DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.68k stars 3.21k forks source link

Cannot Parse JSON #828

Open Gtadictos21 opened 7 months ago

Gtadictos21 commented 7 months ago

Hello team! I am using cJSON in combination with ESP-IDF/FreeRTOS to parse the JSONs that arrive via MQTT, however, every time it receives a JSON, the program crashes because the root object is NULL, i.e., it cannot parse the JSON.

This is the JSON:

{
    "time":"TIMESTAMP",
    "update":{
        "model":"MODEL",
        "version":1.0,
        "deviceId":[
            "00000000-0000-0000-C000-000000000046"
        ],
        "url":"https://example.com/example/"
    }
}

And this is the code:

void get_update_url(char *json) {

    char *jsonModel = NULL;
    char *jsonUrl = NULL;
    double jsonVersion;

    cJSON *update;
    cJSON *root;

    root = cJSON_Parse(json); // <--- It crashes because it can't parse the JSON
    if (root != NULL) 
    {
        update = cJSON_GetObjectItemCaseSensitive(root, "update");
        if (update != NULL) 
        {
            jsonModel = cJSON_GetObjectItemCaseSensitive(update, "model")->valuestring;
            jsonVersion = cJSON_GetObjectItemCaseSensitive(update, "version")->valuedouble;
            jsonUrl = cJSON_GetObjectItemCaseSensitive(update, "url")->valuestring;
        }
    }  
...

This is the output of cJSON_GetErrorPtr():

time': 'TIMESTAMP', 'update': {'model': 'MODEL', 'version': 1.0, 'deviceId': ['00000000-0000-0000-C000-000000000046'], 'url': 'https://example.com/example/'}}

Followed by:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
mbratch commented 7 months ago

What version of cJSON are you using? When you say "crashes" do you mean that cJSON_Parse returned NULL or did it actually crash? Is the JSON you are showing exactly what was passed to the parser?

I tried your JSON object as shown and cJSON_Parse in cJSON version 1.7.15 and version 1.7.17 and parsed it fine.

Gtadictos21 commented 7 months ago

I am using ESP-IDFs cJSON, so I guess it is the last version? I don't know, and it seems I can't check it either. When I say it "crashes", it means that because the root object is NULL, it panics the core, rebooting the ESP32 (Btw, it's a ESP32-S3). The JSON I showed is exactly the same my MQTT broker is sending to the ESP32.

EDIT: I tried with a much simpler script and JSON, and yet again the root object is null.

void parseJSON(char *json)
{
    cJSON *root = cJSON_Parse(json);
    if (root == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        printf("Error: %s\n", error_ptr);
    }

    char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
    printf("JSON KEY: %s\n", value);
}
{"key": "value"}

The cJSON_GetErrorPtr() output is:

Error: key': 'value'}���?
mbratch commented 7 months ago

It's interesting you are seeing a "core panic" since you are able to print an error message after the parser returns NULL. If you are trying to use the NULL value later, that could be reason for the core panic.

I was hoping you could also show the actual string you are passing to the JSON parser. printf it out so it can all be seen. I'm currently suspicious that you have bad characters in the input string which causes the parser to reject the string and return null. Check that the string is properly terminated (your cJSON_GetErrorPtr() output seems to indicate there may be some junk at the end of the string.

Gtadictos21 commented 7 months ago

I think the core panic occurs when calling the cJSON_GetObjectItemCaseSensitive(root,"key")->valuestring; function, since the root object is NULL, so, that makes it read from an empty memory address. Anyway, indeed, it seems to be some junk at the end of the string, how can I remove it?

void parseJSON(char *json)
{
    printf("%s\n", json); // Prints {'key': 'value'}␙�␑
    cJSON *root = cJSON_Parse(json);
    if (root == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        printf("Error: %s\n", error_ptr); // Prints Error: key': 'value'}␙�␑
        return;
    }

    char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
    printf("JSON KEY: %s\n", value);
}

EDIT: I tried by embedding the JSON inside the code and it works! I think I figured out what the problem is.

void parseJSON(char *json)
{   
    char *json2 = "{\"key\":\"value\"}";
    printf("%s\n", json2); // Prints {"key":"value"}
    cJSON *root = cJSON_Parse(json2);
    if (root == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        printf("Error: %s\n", error_ptr);
        return;
    }

    char *value = cJSON_GetObjectItemCaseSensitive(root, "key")->valuestring;
    printf("JSON KEY: %s\n", value); // Prints JSON KEY: value
}

So apparently, the JSON is sent with single quotes {'key': 'value'} by the MQTT broker, and the parser does not recognize it as a string, because C takes single quotes strings as ints.

mbratch commented 7 months ago

So apparently, the JSON is sent with single quotes {'key': 'value'} by the MQTT broker, and the parser does not recognize it as a string, because C takes single quotes strings as ints.

Yes, specifically C uses single quotes for characters or extended characters, which internally are stored as int. It's odd that the MQTT broker is sending JSON using single quotes around strings. According to the syntax description at JSON.org, the strings are to be delineated with double quotes.