ArduinoJson generates to much items in nestedArray

Hi everybody,

I want to publish some internal sensor data from my ESP8266 via ArduinoJSON to a local network.
Therefore I defined my desired JSON syntax:

Desired JSON format:

{
  "date": "2017-12-19",
  "time": "12:15:14",
  "sensor": [
    0.5,
    0.7,
    8.5,
    10.4,
    -5.3,
    4.3
  ],
"alarm": [
  false,
 "2017-12-18",
 "12:10:10"
],
"running_time":"10:15:10",
"battery_status":"100"
}

But this is what I get instead:

{
  "sensor": [
    5.14,
    6.72,
    0.01,
    -7.41,
    8.91,
    4.32,
    5.14,
    6.72,
    0.01,
    -7.41,
    8.91,
    4.32,
    5.14,
    6.72,
    0.01,
    -7.41,
    8.91
  ],
  "alarm": [
    false,
    "2017-12-08",
    "15:13:14",
    false,
    "2017-12-08",
    "15:13:14"
  ],
  "date": "2017-12-19",
  "time": "12:15:14",
  "running_time": "12:18:14",
  "battery_status": "100"
}

Don’t mind the different values of the itmes. The main focus is the problem, that the items of every nestedArray is more than defined.

This is my used code (minimal):

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>

WiFiServer server(80);
WiFiClient client;
const char* ssid = "SSID";
const char* password = "password";

StaticJsonBuffer<400> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonArray& sensor = root.createNestedArray("sensor");
JsonArray& alarm = root.createNestedArray("alarm");


bool readRequest(WiFiClient& client) {
  bool currentLineIsBlank = true;
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      if (c == '\n' && currentLineIsBlank) {
        return true;
      } else if (c == '\n') {
        currentLineIsBlank = true;
      } else if (c != '\r') {
        currentLineIsBlank = false;
      }
    }
  }
  return false;
}

void writeResponse(WiFiClient& client, JsonObject& json) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: application/json");
  client.println("Connection: close");
  client.println();

  json.prettyPrintTo(client);
}

void setup() {

// Wifi Settings
    WiFi.mode(WIFI_STA);
    delay(1000);
    WiFi.begin(ssid, password); 
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
    }
    server.begin();
}



void updateJSON(){


      root["date"] = "2017-12-19";
      root["time"] = "12:15:14";

      sensor.add(5.14);   
      sensor.add(6.72);   
      sensor.add(0.01);   
      sensor.add(-7.41);  
      sensor.add(8.91);   
      sensor.add(4.32);   


      alarm.add(false);
      alarm.add("2017-12-08");
      alarm.add("15:13:14");  

      root["running_time"] = "12:18:14"; 
      root["battery_status"] = "100";    
      root.prettyPrintTo(Serial);

  
}


oid loop() {

  WiFiClient client = server.available();




  // Update JSON object
  updateJSON();



  // store them in a json object
  writeResponse(client, root);

    
}

What should I do, to get the desired count of items of each nestedArray?

Thanks in advance

in every loop updateJSON adds data to arrays. you do not reset the array

Do I have to reset/clear the arrays or the complete buffer?
Which function is neccessary?

Second and subsequent calls could simply set the entries in the array.

Or you could recreate the array each time

There is a remove() method in that library, not sure how it interacts with add though.

Hard to say what you should change. The sketch is a test. You put constants in.