Tells why deserializeJson() or deserializeMsgPack() failed.


A DeserializationError is an enumerated type that can contain one of the following values:

The deserialization succeeded. Cool!
The end of the input is missing. Possible reasons:
  • the input was empty (or it contained only spaces or comments)
  • a buffer was too small to contains the input
  • a timeout occurred when reading from a stream
The input is not recognized. Possible reasons:
The JsonDocument is too small; you need to increase its capacity.
The document included features not supported by the parser. Possible reasons:
  • in JSON, a Unicode surrogate pair like \u0032
  • in MessagePack, a binary value
  • in MessagePack, an object key that is not a string
The nesting limit was reached; you need to increase its value. See deserializeJson(), or ARDUINOJSON_DEFAULT_NESTING_LIMIT for details.


// return a string representation of the error
const char* c_str() const;

// returns the enum value
Code code() const;

How to know where deserialization stopped?

When you pass a Stream to deserializeJson(), it consumes the input but doesn’t print anything to the serial, which makes troubleshooting difficult.

If you want to see what deserializeJson() consumed, use ReadLoggingStream from the StreamUtils library (see example below). Because ArduinoJson stops reading as soon as it sees an error, you can see what caused the error by checking the last consumed character.


Get the error message

DynamicJsonDocument doc(1024);
DeserializationError err = deserializeJson(doc, "!!NOT JSON!!");
if (err) {
    Serial.print(F("deserializeJson() failed: "))

the program above prints:

deserializeJson() failed: InvalidInput


DynamicJsonDocument doc(1024);
DeserializationError err = deserializeJson(doc, "!!NOT JSON!!");
switch (err.code()) {
    case DeserializationError::Ok:
        Serial.print(F("Deserialization succeeded"));
    case DeserializationError::InvalidInput:
        Serial.print(F("Invalid input!"));
    case DeserializationError::NoMemory:
        Serial.print(F("Not enough memory"));
        Serial.print(F("Deserialization failed"));

the program above prints:

Invalid input!

View the content of the input stream

This example requires the StreamUtils library.

Suppose the following line returned InvalidInput:

DeserializationError err = deserializeJson(doc, wifiClient);

If you want to see what caused this error, you can make ArduinoJson log the content of the stream by using a ReadLoggingStream. Replace the above line with:

ReadLoggingStream loggingStream(wifiClient, Serial);
DeserializationError err = deserializeJson(doc, wifiClient);

The first line creates a new Stream on top of wifiClient that writes everything it reads to Serial. Because deserializeJson() stops reading as soon as it sees an error, the last character printed to the serial port is the character that triggered the error.

See also