Using ArduinoJSON to iterate an array ...

I’ve been using ArduinoJSON for a little while, but have found a situation where I can not quite understand how to parse out the nested objects that I need to get access to.

In my example below I have an array of “actions” that can be variable size. In my example there are three objects in the array, but there could be 2 or 5, etc. so I need to find a way to access the array and iterate through them one by one.

I found some sample code on the site that I attempted to use, but it’s not quite working right …

  char responseBody[] = "{"
      "\"status\":\"success\","
      "\"version\":\"1.0\","
      "\"timeEpoch\":1480823123,"
      "\"errorURL\":\"http://ws.error.net/errors\","
      "\"actions\": ["
        "{"
          "\"action\":\"updateProperty\","
          "\"property\":\"some.property.1\","
          "\"value\":\"some value 1\""
        "},"
        "{"
          "\"action\":\"updateProperty\","
          "\"property\":\"some.property.2\","
          "\"value\":\"some value 2\""
        "},"
        "{"
          "\"action\":\"updateProperty\","
          "\"property\":\"some.property.3\","
          "\"value\":\"some value 3\""
        "}"
      "]"
    "}";

  int length = sizeof(responseBody);
  Serial.print(F("JSON response length: "));
  Serial.println(length);
  
  if (length > 1024) {
    Serial.println(F("JSON response size is too large"));
    return false;
  }

  StaticJsonBuffer<1024> jsonBuffer;
  JsonObject& json = jsonBuffer.parseObject(responseBody);

  if (!json.success()) {
    Serial.println(F("Failed to parse responseBody"));
    return false;
  }

  // Parse the top level first ...
  //    "status":"success",
  //    "version":"1.0",
  //    "timeEpoch":1480823123,
  //    "errorULR":"http://ws.error.net/errors",

  //  test for missing property
  Serial.print("Bogus: ");
  if (json.containsKey("bogus"))
  {
    json["bogus"].printTo(Serial);
    Serial.println();
  } else {
    Serial.println("missing");
  }

  Serial.print("Status: ");
  if (json.containsKey("status"))
  {
    json["status"].printTo(Serial);
    Serial.println();
  } else {
    Serial.println("missing");
  }

  Serial.print("Version: ");
  if (json.containsKey("version"))
  {
    json["version"].printTo(Serial);
    Serial.println();
  } else {
    Serial.println("missing");
  }

  Serial.print("TimeEpoch: ");
  if (json.containsKey("timeEpoch"))
  {
    json["timeEpoch"].printTo(Serial);
    Serial.println();
  } else {
    Serial.println("missing");
  }

  Serial.print("Error URL: ");
  if (json.containsKey("errorURL"))
  {
    json["errorURL"].printTo(Serial);
    Serial.println();
  } else {
    Serial.println("missing");
  }

  // now iterate the actions array
  Serial.println("Iterate actions ...");
  JsonObject& actions =  json["actions"];
  for (auto dataobj : actions){
      Serial.println(dataobj.key);
  }

  Serial.println("Done!");

Nothing prints when I try to iterate the “actions” and access those objects …

… what am I doing wrong?

Thanks in advance!

Ha! Of course posting this got me to my first issue!!

JsonObject& actions =  json["actions"];

should really be:

JsonArray& actions =  json["actions"];

I'm getting further ... and now have my objects printing ...