Description

Sets the value at specified key.

Signatures

bool set(TString key, bool value);
bool set(TString key, float value);
bool set(TString key, double value);
bool set(TString key, signed char value);
bool set(TString key, signed long value);
bool set(TString key, signed int value);
bool set(TString key, signed short value);
bool set(TString key, unsigned char value);
bool set(TString key, unsigned long value);
bool set(TString key, unsigned int value);
bool set(TString key, unsigned short value);
bool set(TString key, const char *value);
bool set(TString key, char *value); // see Remarks
bool set(TString key, const String &value); // see Remarks
bool set(TString key, const std::string &value); // see Remarks
bool set(TString key, const __FlashStringHelper* value); // see Remarks
bool set(TString key, JsonArray &array);
bool set(TString key, JsonObject &object);
bool set(TString key, const JsonVariant &value);

Arguments

key: the key to attach the value to, can be a:

  • const char*,
  • String,
  • std::string, or
  • const __FlashStringHelper*.

value: the value to attach to the key.

Return value

true if allocation succeeded.

false if there was not enough space left in the JsonDocument.

Remarks

ArduinoJson makes a copy of the string when you call this function with one of the following types:

  • char*
  • String (or std::string)
  • const __FlashStringHelper * (i.e. Flash string)

This duplication causes the JsonBuffer to grow. The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.

Note that the rules changed in version 5.13.

Example

StaticJsonDocument<200> doc;
JsonObject object = doc.to<JsonObject>();
object.set("hello","world");
serializeJson(object, Serial);

will print the following string to the serial output:

{"hello":"world"}

See also