ArduinoJson 6: passing JSON document to function reports unknown type

Hi all!

I am working on in Arduino IDE 2.3.4 using ArduinoJson library 6.21.5 and might be considered a newbie in C/C++ .

To better structure my code, I am trying to encapsulate conversion of a struct to JSON in a function. However, whatever I try, it does not seem to accept a JsonObject as parameter.
Here is the relevant sample code:

#include <ArduinoJson.h>

void checkTypesAreThere() {
  JsonObject jObj;
  jObj["sensor_id"] = "1";
}

void convertConfigToJson1(JsonObject jObj) {
  // empty for now
}

void convertConfigToJson2(JsonObject& jObj) {
  // empty for now
}

void convertConfigToJson2(JsonObject* jObj) {
  // empty for now
}

Trying to compile this, causes errors:

/JSON.ino:8:6: error: variable or field 'convertConfigToJson1' declared void
    8 | void convertConfigToJson1(JsonObject jObj) {
/JSON.ino:8:27: error: 'JsonObject' was not declared in this scope
    8 | void convertConfigToJson1(JsonObject jObj) {
/JSON.ino:12:6: error: variable or field 'convertConfigToJson2' declared void
   12 | void convertConfigToJson2(JsonObject& jObj) {
/JSON.ino:12:27: error: 'JsonObject' was not declared in this scope
   12 | void convertConfigToJson2(JsonObject& jObj) {
/JSON.ino:12:39: error: 'jObj' was not declared in this scope
   12 | void convertConfigToJson2(JsonObject& jObj) {
/JSON.ino:16:6: error: variable or field 'convertConfigToJson3' declared void
   16 | void convertConfigToJson3(JsonObject* jObj) {
/JSON.ino:16:27: error: 'JsonObject' was not declared in this scope
   16 | void convertConfigToJson3(JsonObject* jObj) {
/JSON.ino:16:39: error: 'jObj' was not declared in this scope
   16 | void convertConfigToJson3(JsonObject* jObj) {

Note: I had originally been using ArdunioJson v7.3 and the error messages are the very same

Thanks in advance for any help! I am utterly clueless at this point :frowning:

I think you need a JsonDocument to create a JsonObject

Because the JsonObject is a reference, you need a JsonDocument to create an object. See the example below.

That's not the point (and I am not sure it's generally true either - afaik the JsonDocument is a reference to either a JsonObject or JsonArray, dependent on usage). I am not getting some error at runtime because I try to create a JsonObject. Neither am I getting a compile time error that tells me I cannot create a JsonObject (as done in the checkTypesAreThere function).

The issue are the 3 different convertConfigToJson functions, where I try to pass in a JsonObject as parameter, that cause errors at compile time already

Which board are you compiling for? I do not get any error messages when compiling for an UNO with ArduinoJson 7.3.0 in Arduino IDE 2.3.4 or 1.8.19, only warnings that the function argument is unused.

1 Like

Works for me on an Uno:

#include <ArduinoJson.h>

void convertConfigToJson1(JsonObject jObj) {
  Serial.println("Pass by Copy");
}

void convertConfigToJson2(JsonObject& jObj) {
  Serial.println("Pass by Reference");
}

void convertConfigToJson3(JsonObject* jObj) {
  Serial.println("Pass by Pointer");
}

void checkTypesAreThere() {
  JsonObject jObj;
  jObj["sensor_id"] = "1";
  convertConfigToJson1(jObj);
  convertConfigToJson2(jObj);
  convertConfigToJson3(&jObj);
}


void setup() {
  Serial.begin(115200);
  delay(1000);
  checkTypesAreThere();
}

void loop() {
}

Output:

Pass by Copy
Pass by Reference
Pass by Pointer
1 Like

I am compiling for a ESP8266 NodeMCU (ESP-12E).

Ok, so this is indeed not "wrong code" by me but rather an issue introduce by the board specific pre-processor (or whatever this is called).

Guess then I cannot do anything about it :frowning:

Your code is not wrong by syntax, but is absolutely non-sense as a program.

Did you try to compile @gfvalvo's code on NodeMCU?

The sense of the program code I posted is to allow people willing and able to help, to read relevant parts (and only those) and at the same to to reproduce (or, in this case, conclude they actually cannot reproduce) the issue. And as you can see, it totally fulfilled it's purpose. What would another 500 lines of code help? They make it only harder to spot possible syntax issues or whatever (which I have not been able to rule out due to my lack of experience with C/C++).

Anyway, in case someone stumbles into something similar at some point, I by now can add that I got it to compile (on ESP8266). In an effort of refactoring and cleaning up code, I refactored the original procedural approach to an object oriented one. While it would not compile the above functions, it would all of a sudden compile those when being functions of a class. So

void Configuration::toJson(JsonDocument& doc) {
//....
}

did compile :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.