DynamicJsonDocument was not declared in this scope.

Hello, I have a simple sketch where it sends a JSON object to the serial which will be later used by node.js. I followed a couple of tutorials and updated the code due to the changes in the JSON library.

I am getting DynamicJsonDocument was not declared in this scope. error but I can't understand why.

Code used for the arduino.

#include <Arduino_JSON.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.read() == 'j'){
    DynamicJsonDocument doc(2048);
    JsonObject root = jsonBuffer.to<JsonObject>();

    doc["day"] = "Sunday";
    doc["month"] = "September";
    doc["year"] = "2020";

    serializeJsonPretty(doc, Serial);
    Serial.println();
  }

}

ERROR

C:\Users\S\Desktop\sketch_sep06a\sketch_sep06a.ino: In function 'void loop()':
sketch_sep06a:12:23: error: 'jsonBuffer' was not declared in this scope
JsonObject root = jsonBuffer.to();
^~~~~~~~~~
C:\Users\S\Desktop\sketch_sep06a\sketch_sep06a.ino:12:23: note: suggested alternative: 'JsonBuffer'
JsonObject root = jsonBuffer.to();
^~~~~~~~~~
JsonBuffer
sketch_sep06a:12:47: error: expected primary-expression before '>' token
JsonObject root = jsonBuffer.to();
^
sketch_sep06a:12:49: error: expected primary-expression before ')' token
JsonObject root = jsonBuffer.to();
^
exit status 1
'jsonBuffer' was not declared in this scope

Any help would be greatly appreciated and thank you for taking the time.

Please post the full error message

Sorry, I added the error now.

This error means you're using a version of the "ArduinoJson" library that is not compatible with this code. The "ArduinoJson" library 6.x.x versions have breaking changes that make them not work with code written for the "ArduinoJson" 5.x.x versions.

The recommended solution is to roll back to the newest 5.x.x release of ArduinoJson:

  1. Select Sketch > Include Library > Manage Libraries... from the Arduino IDE menus.
  2. Wait for the download to finish.
  3. In the "Filter your search..." box, type "arduinojson".
  4. In the search results, click on "ArduinoJson by Benoit Blanchon"
  5. From the dropdown version menu, select "5.13.5".
  6. Click the Install button.
  7. Wait for the installation to finish.
  8. Click the Close button.

The code should now compile.

The alternative solution is to update the code to work with the 6.x.x versions of ArduinoJson. You will find a guide to this here:

Reference:

1 Like

Thank you very much, I'll do as advised. Thanks :smiley:

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per

Alright, I found my problem and it's hilariously sad. I have 2 JSON libraries installed and I was including the wrong one. I knew about the updated version so I taught they changed the wording.

I downgraded as advised and it works like a charm, I'll look into upgrading my code to the final version later.

Thank you very much for the help and step by step instructions.

Thanks!

You're welcome. I'm glad to hear it's working now. Enjoy!
Per