DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.28k stars 3.15k forks source link

JSON Parse Error #830

Closed pp-aravind closed 2 months ago

pp-aravind commented 4 months ago

Hello team! I am using cJSON in combination with STM32/FreeRTOS to parse the JSONs that arrive from NFC NDEF data. And this is working most of the time, but sometimes I get JSON parse error occurred at exactly 3rd object and this JSON data is worked previously. This is the code:

void JsonDataParse(const char *theText)
{
      cJSON *json = cJSON_Parse(theText);
      if(json == NULL)
      {
        const char *error_ptr =  cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
          DebugLog(LOG_LVL_ERR,"Error before:%s\r\n" ,error_ptr);
        }
        DebugLog(LOG_LVL_ERR,"Error\r\n");
      }
}  

This is the debug log from the STM32 that we are getting

The text for the record is {"t":"6","w":"arianlajimi","p":"khoonema","u":"http://52.165.195.1:8080/?message="} Error before:,"p":"khoonema","u":"http://52.165.195.1:8080/?message="} Error

The text for the record is {"t":"4","j":"0000000000000003","a":"83E923866D4C4D5C05AF430AF7110F11"} Error before:,"a":"83E923866D4C4D5C05AF430AF7110F11"} Error

And these JSON packets are working fine almost most of the time but sometimes we are getting this JSON parse error

mbratch commented 4 months ago

The JSON examples you show look correct. Perhaps there's a hidden non-printable character in the string coming from the NFC NDEF, or perhaps from noise on the communication channel? When you receive the string, before you parse it, can you print it out in hex bytes and check?

pp-aravind commented 4 months ago

Thanks for the reply. The problem is this issue comes at random times, If once it is reproduced I can share those logs

pp-aravind commented 4 months ago

Hi, We got JSON parse error again and we check the hex data and seems it is fine and there are no hidden non printable characters, here is the Log

The text for the record is {"w":"GWiFi","p":"G@dge@n#24it&dp","t":"0,0,26941480,140,170,40,40,100,200,150,29,39,89,45,2,75,75,2"}

The Hex Data of JSON: { 7B 22 77 22 3A 22 47 57 69 46 69 22 2C 22 70 22 3A 22 47 40 64 67 65 40 6E 23 32 34 69 74 26 64 70 22 2C 22 74 22 3A 22 30 2C 30 2C 32 36 39 34 31 34 38 30 2C 31 34 30 2C 31 37 30 2C 34 30 2C 34 30 2C 31 30 30 2C 32 30 30 2C 31 35 30 2C 32 39 2C 33 39 2C 38 39 2C 34 35 2C 32 2C 37 35 2C 37 35 2C 32 22 7D }

Error before:0,0,26941480,140,170,40,40,100,200,150,29,39,89,45,2,75,75,2"}

Error in json parsing

mbratch commented 4 months ago

I used the current version of cJSON to parse your string and it did not give an error. The following code prints "JSON ok":

const char *j = "{\"w\":\"GWiFi\",\"p\":\"G@dge@n#24it&dp\",\"t\":\"0,0,26941480,140,170,40,40,100,200,150,29,39,89,45,2,75,75,2\"}";
cJSON * jo = cJSON_Parse(j);

if (jo) {
    printf("JSON ok\n");
else
    printf("JSON invalid\n");
pp-aravind commented 4 months ago

While testing we seen that sometimes we are getting JSON parse error at exactly 3rd object being gone to parse. As you can see on the above logs that I shared the error occurred at object "t" is being gone to extract.

pp-aravind commented 4 months ago

For your info Our code is implemented in FreeRTOS on STM32 and the cJSON functions are only called in one thread and not being used in multithread.

daschfg commented 4 months ago

I don't think this is an issue with cJSON.

Just a wild guess: Depending on your STM32 model, maybe this could be related to caching? Are you sure your data is complete the moment you start parsing it?

pp-aravind commented 4 months ago

We are collecting the whole NFC JSON packets before parsing it.

pp-aravind commented 3 months ago

Hi, In our code we put more debug logs in cJSON.c file and these are the error logs that we got when we gave valid json to parse, and we are getting error at exact 3rd object when it goes for parsing. This Json has been working for sometimes but after it is getting unable parse.

The text for the record is {"w":"4SightPatrolWiFi","p":"Backprop1$","t":"0,0,37380866,140,170,40,40,100,200,10,1,39,88,45,2,75,91,1"}

The Hex Data of JSON: {7b 22 77 22 3a 22 34 53 69 67 68 74 50 61 74 72 6f 6c 57 69 46 69 22 2c 22 70 22 3a 22 42 61 63 6b 70 72 6f 70 31 24 22 2c 22 74 22 3a 22 30 2c 30 2c 33 37 33 38 30 38 36 36 2c 31 34 30 2c 31 37 30 2c 34 30 2c 34 30 2c 31 30 30 2c 32 30 30 2c 31 30 2c 31 2c 33 39 2c 38 38 2c 34 35 2c 32 2c 37 35 2c 39 31 2c 31 22 7d}

The Hex Data of JSON Length = 106 JSON Text Length = 107 Object JSON Object JSON Parsing:

"w":"4SightPatrolWiFi","p":"Backprop1$","t":"0,0,37380866,140,170,40,40,100,200,10,1,39,88,45,2,75,91,1"}

String JSON output - w String JSON String JSON output - 4SightPatrolWiFi

Object JSON Parsing: "p":"Backprop1$","t":"0,0,37380866,140,170,40,40,100,200,10,1,39,88,45,2,75,91,1"}

String JSON output - p String JSON String JSON output - Backprop1$

.0022681585:INFO :Object JSON Parsing: "t":"0,0,37380866,140,170,40,40,100,200,10,1,39,88,45,2,75,91,1"}

String JSON output - t String JSON String JSON input_pointer Failed - 0,0,37380866,140,170,40,40,100,200,10,1,39,88,45,2,75,91,1"}

Object JSON Failed

cJSON_ParseWithLengthOpts Failed, Item is not NULL cJSON_ParseWithLengthOpts Failed, Value is not NULL Error before:0,0,37380866,140,170,40,40,100,200,10,1,39,88,45,2,75,91,1"}

Error Error in json parsing

rrvirtual commented 2 months ago

Hi! "This Json has been working for sometimes but after it is getting unable parse."

Are you deleting the pointer? p_json = cJSON_Parse(p_aux_msg); "cJSON_Delete(p_json)"?

pp-aravind commented 2 months ago

Hi all, Thanks for you support and sorry for the late reply. The issue has been resolved, there is a cJSON pointer that hasn't been freed, so after sometimes the memory leak issue occurs and that leads to the json parse error issue.