ArduinoJson setting/updating json object values

Hello :slight_smile:

I have a Json document with an object that I need to update the values every few seconds (sensor values).

I have made key value pairs using doc[“key”] = “value” which generates the document as desired. If I place this in the loop function and set the value to randomly generated values the document does not update but adds new objects for each iteration… how do i set the values not add new ones? i have also tried to clear the document which has not worked.

StaticJsonDocument<256> doc1;
  StaticJsonDocument<256> doc2;
//JsonObject obj = doc1.to<JsonObject>();

void loop(void) {
  server.handleClient();
  MDNS.update();

  doc1["BEMUID"] = "123456";
  doc1["voltage"]= ((rand()%(52-48+1))+48);
  doc1["current"] = ((rand()%(20+20+1))-20);
  doc1["charge"] = rand()%(101);
  //JsonArray cellArray = doc1.createNestedArray("cellArray"); //not working
  
  for (i=0; i<16; i++){
  cellArray[i] = ((rand()%(maxV-minV+1))+minV)/ten;
  }
  copyArray(cellArray, doc2.to<JsonArray>());
  doc1["cell voltages"] = doc2;
  serializeJson(doc1, output);

  server.on("/values.json", sendValues);
  delay(1500);  
}

Thanks x

The problem must be in your code. Please post it in code tags, to make it forum compatible.

I realized it was because I was serializing it to the output string and not clearing the string for each iteration.