JSONValue False Positives

Hello,

I am parsing JSON strings using the deviceOS libraries as recommended in the Particle reference documentation, I believe there is an error with its ability to check if a JSON is valid.

Please see below where I provide a mangled JSON packet.

//   The string below minus escape characters:
//   "{"Bad data":3298{"Good data":3298,"Good data":-626136,"Good data":22709}";

String Data_Receive_buf = "{\"Bad data\":3298{\"Good data\":3298,\"Good data\":-626136,\"Good data\":22709}";

JSONValue outerObj = JSONValue::parseCopy(Data_Receive_buf);

if (outerObj.isValid() == true) {
    Particle.publish("Uh oh");
    Particle.publish(Data_Receive_buf);
}

Result

…

Can someone confirm this bug? It also occurs with a JSON like the one below:
“;{“Good data”:3298,“Good data”:-626136,“Good data”:22709}”

A hint at the problem might have to do with the fact that outerObj.count() returns 3. Somehow it just ignores the first bad characters.

Hi,
It’s definitely not a bug it’s a construction of your JSON as you can’t include nested JSON in JSON when the nested part is not a value of some key also using the same name for key/value pairs is bad bad bad as you can’t distinguish between the same mame of keys. Also when you build your key with space the access for the value of that key is not as easy as with construction with eg: underscore symbol “_”.
So even if JSON validator indicate that your JSON is valid:

it’s actually not, a quick js. test shows that your JSON is not valid:

Sorry Dreamer.

The Bad JSON formatting in on purpose. To clarify my question, why doesnt the JSONValue recognize that its a bad format? I feel that the class should not present itself as “valid” if there is a nested JSON.

My actual key value pairs dont included spaces or repeated values. But I am receiving this JSON over serial. So sometimes it gets broken in the transmission. I want to be able to find whether the JSON is valid prior to using it.

Without getting into why the JSON parser may be too forgiving, perhaps erroneously so, you shouldn’t be depending on the JSON parser to catch corrupted JSON frames due to a bad serial link.

Serial corruption might turn your valid JSON into invalid JSON. But it also might corrupt into still valid JSON such as "nuclear_launch_detected":0 into nuclear_launch_detected":1 and cause World War III.

So embed your JSON in some additional framing with a CRC32 (at minimum) to detect/reject serial corruption. Don’t break layering and expect the JSON parser to validate your serial connection.

2 Likes