ArduinoJson Nested Array - Programming Questions - Arduino Forum Home » Arduino Json 6 Nested Array » ArduinoJson Nested Array - Programming Questions - Arduino Forum Maybe your like Arduino Ky-008 Laser Sensor Module Arduino Ky-013 Temperature Sensor Module Arduino Ky-018 Photoresistor Module Arduino Ky-026 Flame Sensor Module Arduino Ky-037 Sensitive Microphone Sensor Module ArduinoJson nested array Projects Programming gilbert54 October 24, 2020, 1:26pm 1 So far, ArduinoJson behaves as expected, but now I'm getting an unexpected result. Here is the code: void clearAllErrors() { // fake error counters for testing errorCounters[1] = 1; errorCounters[5] = 1; errorCounters[9] = 1; jsonDoc.clear(); // wipe all elements in existing JSON document jsonDoc["RESULT"] = 200; // return code = OK JsonArray json_clearedErrors = jsonDoc.createNestedArray("CLEARED"); // array contains all cleared errors JsonObject json_ErrorCounter = jsonDoc.createNestedObject("COUNTER"); // JSON objects for NAME and VALUE of the counter uint8_t errIndex = 0; for(uint8_t errIndex = 0; errIndex < SIZE_ERRCOUNTERS; errIndex++) { // loop through error counters if(errorCounters[errIndex] > 0) { // only for counters which are non-zero json_ErrorCounter["NAME"] = errorCounterNames[errIndex]; // name of the counter json_ErrorCounter["VALUE"] = errorCounters[errIndex]; // error count json_clearedErrors.add(json_ErrorCounter); // add JSON object to array errorCounters[errIndex] = 0; // clear the error counter } } // TODO: save the cleared counters to NVS respond2Client(); // send this jsonDoc as response } Here is the output from that code (in Serial monitor): { "RESULT": 200, "CLEARED": [ { "NAME": "UpperFloatStuck", "VALUE": 1 }, { "NAME": "LowerFloatStuckHigh", "VALUE": 1 }, { "NAME": "HeaterError", "VALUE": 1 } ], "COUNTER": { "NAME": "HeaterError", "VALUE": 1 } } The content of the array is as expected, but I don't understand where the last JSON object "COUNTER" is coming from. The jsonDoc appears to add an extra copy of the last array element. If I clear all counters and run the program again, I get an empty array and no extra's (which is what I expect). I also tried this with the declaration of the nestedObject "COUNTER" inside the 'for' loop, but that doesn't make any difference. wildbill October 24, 2020, 1:47pm 2 You're using json_ErrorCounter as a temporary variable to build each array element before adding it to the array, but when you created it, you added it to the json_clearedErrors object after the array. Apparently, you didn't really want to do that. Problem setting up a "data class" for storing POST data gilbert54 October 24, 2020, 2:03pm 3 Thanks, Bill. I re-read the documentation and figured it out. I was indeed creating an JsonObject which was appended to the root AND to the array, which is not what I intended to do. The modified code which works as intended: void clearAllErrors() { // fake error counters for testing errorCounters[1] = 1; errorCounters[5] = 1; errorCounters[9] = 1; jsonDoc.clear(); // wipe all elements in existing JSON document jsonDoc["RESULT"] = 200; // return code = OK JsonArray json_clearedErrors = jsonDoc.createNestedArray("CLEARED"); // array contains all cleared errors uint8_t errIndex = 0; for(uint8_t errIndex = 0; errIndex < SIZE_ERRCOUNTERS; errIndex++) { // loop through error counters if(errorCounters[errIndex] > 0) { // only for counters which are non-zero JsonObject json_ErrorCounter = json_clearedErrors.createNestedObject(); // JSON objects for NAME and VALUE of the counter json_ErrorCounter["NAME"] = errorCounterNames[errIndex]; // name of the counter json_ErrorCounter["VALUE"] = errorCounters[errIndex]; // error count errorCounters[errIndex] = 0; // clear the error counter } } // TODO: save the cleared counters to NVS respond2Client(); // send this jsonDoc as response } Related topics Topic Replies Views Activity ArduinoJson generates to much items in nestedArray Programming 5 1200 May 5, 2021 Arduino json library problem (or my CPP indolence) Programming 6 1078 May 5, 2021 JSON nested array synax Programming 4 734 October 2, 2023 Using ArduinoJSON to iterate an array ... Programming 2 9304 May 5, 2021 ArduinoJson (V7): How to serialize into Json object the array of struct? Programming 6 2068 October 3, 2024 Tag » Arduino Json 6 Nested Array JsonDocument::createNestedArray() | ArduinoJson 6 JsonArray | ArduinoJson 6 How To Create Nested Json Using ArduinoJson On ESP8266 Nested Array Can Not Contain Other Arrays? · Issue #51 - GitHub How To Create Nested Object In Nested Array? · Issue #380 - GitHub ESP8266: Parsing JSON Arrays - Techtutorialsx C++ ArduinoJson::JsonArray::createNestedObject() - CPPSECRETS [தமிழ்] Create JSON String With Nested JSON & JSON Array Mastering ArduinoJson 6 - PDFCOFFEE.COM Arduino -Sending And Receiving JSON Data Over MQTT Fun With ESP32 + Arduino (15) ArduinoJSON Library (V6 Version) Decoding And Encoding JSON Arduino | Random Nerd Tutorials ArduinoJSON: Serialize And Deserialize - Tutorialspoint How To Use A Multidimensional Json Array In Android Studio ...