Multiple JSON entries

Hi everyone

Still trying to wrap my head around everything, and now I'm stuck wit the JSON library.

I'm using the JsonConfigFile example, and would like to alter the example to contain multiple entries (events i my case) like this:

{
  "version": 3.1,
  "Event": [
    {
      "ID": "{bb9981f1-ba21-4317-ab43-7bc8da37ff4a}",
      "Hooray": 7,
      "Start": 1256953732,
      "End": 2256953732
    },
    {
      "ID": "{bb9981f1-cafe-4317-ab43-7bc8da37ff4a}",
      "Hooray": 3,
      "Start": 1456953732,
      "End": 2456953732
    }
  ]
}

A hint on where to start on this one? :slight_smile:

Best regards, Sticky

Well, you could start here: https://www.json.org/.

If you want multiple entries, then you are looking at using an array. Technically (technically), the outermost thing must be an object, so you can make ot an object with one field named 'event', which is an array of events:

{
  event: [
    { place: "my place", time: 1200 },
    { place: "down 't mall", time: 1330 },
    { place: "my place", time: 1600}
  ]
}
  root["Version"] = 1.2;
  JsonArray& events = root.createNestedArray("Events");

  JsonObject& event = events.createNestedObject();
  event["id"] = "{bb9981f1-ba21-4317-ab43-7bc8da37ff4a}";
  event["hooray"] = 7;
  event["start"] = 1456953732;
  event["end"] = 1356953732;

  event& = events.createNestedObject();
  event["id"] = "{bb9981f1-cafe-4317-ab43-7bc8da37ff4a}";
  event["hooray"] = 16;
  event["start"] = 1556953732;
  event["end"] = 1656953732;

.. which gives an error: use of deleted function 'ArduinoJson::JsonObject& ArduinoJson::JsonObject::operator=(const ArduinoJson::JsonObject&)'

Fair enough. I then found this: How to assign a JsonArray or JsonObject .. but I having trouble with fixing my code with the "use pointer instead of reference" tip. A bit of help please? :slight_smile:

This:

 JsonObject& event = events.createNestedObject();

Declares event to be a reference to a JsonObject, and assigns it. You probably actually need a pointer

 JsonObject *event = events.createNestedObject();

You'll need to free those objects being created, or the sketch will use up all the memory and crash. See the json library documentation on how to do that.

Did try that already .. but it just blew up in my face. :slight_smile:

JsonConfigFile\JsonConfigFile.ino: In function 'void saveConfiguration(const char*)':

JsonConfigFile:85: error: cannot convert 'ArduinoJson::JsonObject' to 'ArduinoJson::JsonObject*' in initialization

   JsonObject *event = events.createNestedObject();

                                                 ^

JsonConfigFile:86: error: invalid types 'ArduinoJson::JsonObject*[const char [3]]' for array subscript

   event["id"] = "{bb9981f1-ba21-4317-ab43-7bc8da37ff4a}";

             ^

JsonConfigFile:87: error: invalid types 'ArduinoJson::JsonObject*[const char [6]]' for array subscript

   event["relay"] = 7;

                ^

JsonConfigFile:88: error: invalid types 'ArduinoJson::JsonObject*[const char [6]]' for array subscript

   event["start"] = 1456953732;

                ^

JsonConfigFile:89: error: invalid types 'ArduinoJson::JsonObject*[const char [4]]' for array subscript

   event["end"] = 1356953732;

              ^

JsonConfigFile:91: error: cannot convert 'ArduinoJson::JsonObject' to 'ArduinoJson::JsonObject*' in assignment

   event = events.createNestedObject();

         ^

JsonConfigFile:92: error: invalid types 'ArduinoJson::JsonObject*[const char [3]]' for array subscript

   event["id"] = "{bb9981f1-cafe-4317-ab43-7bc8da37ff4a}";

             ^

JsonConfigFile:93: error: invalid types 'ArduinoJson::JsonObject*[const char [6]]' for array subscript

   event["relay"] = 16;

                ^

JsonConfigFile:94: error: invalid types 'ArduinoJson::JsonObject*[const char [6]]' for array subscript

   event["start"] = 1556953732;

                ^

JsonConfigFile:95: error: invalid types 'ArduinoJson::JsonObject*[const char [4]]' for array subscript

   event["end"] = 1656953732;

              ^

exit status 1
cannot convert 'ArduinoJson::JsonObject' to 'ArduinoJson::JsonObject*' in initialization