Description

serializeJson() serializes a JsonDocument to create a minified JSON document, i.e. a document without spaces or line break between values.

If you want a prettified JSON document, use serializeJsonPretty()

Signatures

serializeJson(const JsonDocument& doc, char* output, size_t outputSize);
serializeJson(const JsonDocument& doc, char output[size]);
serializeJson(const JsonDocument& doc, Print& output);
serializeJson(const JsonDocument& doc, String& output);
serializeJson(const JsonDocument& doc, std::string& output);
serializeJson(const JsonDocument& doc, std::ostream& output);

Arguments

  • doc: the JsonDocument to serialize (in fact, you can also use a JsonArray, a JsonObject, or a JsonVariant)
  • output: the destination where the JSON document should be written
  • outputSize: the capacity of the destination buffer

Because output can be any implementation of Print, you can uses instances like Serial, EthernetClient, WifiClient

This function treats String and std::string as streams: it doesn’t replace the content, it appends to the end.

Return value

The number of bytes written.

Configuration

serializeJson() can be configured with the following settings:

Example

StaticJsonDocument<200> doc;
doc["hello"] = "world";
serializeJson(doc, Serial);

will write the following string to the serial port:

{"hello":"world"}

See also