ArduinoJson Truncating String Literals

I've used ArduinoJson with the Photon 2 successfully before, but today I ran into a weird bug where JsonObject truncates string literals to just one character (when the string literals are stored as keys or values).

Here is the code:

    JsonDocument doc;
    String jsonString;

    JsonObject obj0 = doc.createNestedObject();

    obj0["key"] = "Temperature (F)";
    obj0["value"] = random(60, 100);

    serializeJson(doc, jsonString);

    Serial.println(jsonString);

    delay(5000);

and here is the output:

[{"k":"T","v":84}]

However, if I change the code to replace the string literals with string variables, it works correclty. For example, with this code

    String strKey = "key";
    String strValue = "value";
    String strTemp = "Temperature (F)";
    obj0[strKey] = strTemp;
    obj0[strValue] = random(60, 100);

I get the expected output

[{"key":"Temperature (F)","value":76}]

Any one have any idea why this is happening, or a better workaround?

Thanks!

That's really weird and seems like a bug in the library. This shouldn't be necessary, but just out of curiosity, what does it do for this:

obj0[String("key")] = String("Temperature (F)");
1 Like

That seems like a workaround @rickkas7 . With the explicit String(...) I get the correct output for JSON

[{"key":"Temperature (F)","value":89}]

It definitely seems like a bug. I really appreciate your quick response.