Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Parse JSON #828

Open
Gtadictos21 opened this issue Feb 11, 2024 · 5 comments
Open

Cannot Parse JSON #828

Gtadictos21 opened this issue Feb 11, 2024 · 5 comments

Comments

@Gtadictos21
Copy link

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
Copy link

mbratch commented Feb 11, 2024

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
Copy link
Author

Gtadictos21 commented Feb 11, 2024

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
Copy link

mbratch commented Feb 12, 2024

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
Copy link
Author

Gtadictos21 commented Feb 12, 2024

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
Copy link

mbratch commented Feb 18, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants