ArduinoJson (V7): How to serialize into Json object the array of struct?

I have array of struct

#include <ArduinoJson.h>

JsonDocument JSON_Obj;

struct MyStruct { int index; const char* EffectName; bool Selected; };

MyStruct MyArray[] = 
  {
    {1, "First",  false},
    {5, "Second", true},
    {3, "Third",  false},
  };

How to serialize it into Json object, using ArduinoJson library V7 (by Benoit Blanchon) ?
That is what I've tried, but it is good only for one array item, do not know how to iterate over array...

JsonArray data = JSON_Obj["Array"].to<JsonArray>();
data.add(MyArray[0].index);
data.add(MyArray[0].EffectName);
data.add(MyArray[0].Selected);

P.S. Need to pass it over web-socket to parse on remote site by java-script to update options on "select" html control...

try something like this

#include <ArduinoJson.h>

JsonDocument doc;

struct MyStruct { int index; const char* EffectName; bool Selected; };

MyStruct myArray[] = 
{
    {1, "First",  false},
    {5, "Second", true},
    {3, "Third",  false},
};
const size_t myArrayCnt = sizeof myArray / sizeof * myArray;

void setup() {
  Serial.begin(115200);

  for (MyStruct& s: myArray) {
    JsonObject obj = doc.createNestedObject();
    obj["index"] = s.index;
    obj["EffectName"] = s.EffectName;
    obj["Selected"] =s.Selected;
  }

  // Serialize JSON object to string
  String jsonString;
  serializeJson(doc, jsonString);

  Serial.println(jsonString);
}

void loop() {}

Yes, it works, thank you!

[
  {
    "index": 1,
    "EffectName": "First",
    "Selected": false
  },
  {
    "index": 5,
    "EffectName": "Second",
    "Selected": true
  },
  {
    "index": 3,
    "EffectName": "Third",
    "Selected": false
  }
]

But it creates whole section with no name, are there any way to add the name, like "Options": in front of this?

Ok, seems like I need to explain problem better.

Your solution (see above) works good if I have ONLY this array in json. If I have added some plain variable either before or after array - array completely disappear from json... Look on the code below, it gives me only: {"Var1":"ABCD","Var2":"before array","Var3":"after array"}.
Array appears in json only if I remove all other variables adding into json, before and after array.

So, my corrected question is: how to add array into json object along with other variables?

#include <ArduinoJson.h>

JsonDocument JSON_Obj;

struct MyStruct { int index; const char* EffectName; bool Selected; };

MyStruct MyArray[] = 
  {
    {1, "First",  false},
    {5, "Second", true},
    {3, "Third",  false},
  };

void setup() 
{
  Serial.begin(115200);
  delay(1000); //for stability
  Serial.println(); Serial.println();

  //Add plain variables
  JSON_Obj["Var1"] = String("ABCD");
  JSON_Obj["Var2"] = String("before array");
 

  //Add Array 
  for (MyStruct& s: MyArray) {
    JsonObject obj = JSON_Obj.createNestedObject();
    obj["index"] = s.index;
    obj["EffectName"] = s.EffectName;
    obj["Selected"] =s.Selected;
  }

  //add more plain variables
  JSON_Obj["Var3"] = String("After array"); 

String jsonString;

serializeJson(JSON_Obj, jsonString);
Serial.println(jsonString);
}

void loop() 
{ 
}

I have done it in two different ways,:

  1. using JsonObject as object for whole array and JsonArray for each array item;
  2. using JsonArray bor both, for whole array and for each item.

See code below, you can comment out defines on the beginning to control which way to use...

#include <ArduinoJson.h>

JsonDocument JSON_Obj;

struct MyStruct { int index; const char* EffectName; bool Selected; };

MyStruct MyArray[] = 
  {
    {1, "First",  false},
    {5, "Second", true},
    {3, "Third",  false},
  };

const size_t MyArrayCnt = sizeof MyArray / sizeof * MyArray;

#define ExtObject_IntArray
//#define ExtObject_IntObject

void setup() 
{
  Serial.begin(115200);
  delay(1000); //for stability
  Serial.println();Serial.println();

  //Added plain variables
  JSON_Obj["Var2"] = String("before array");
 
#ifdef ExtObject_IntArray
  //External JsonObject, Internal JsonArray 
  JsonObject ExtObj1 = JSON_Obj["ArrayName1"].createNestedObject();  
  for (uint8_t i=0; i<MyArrayCnt; i++) 
    {
    JsonArray IntArr1 = ExtObj1["ArrayIndex"+String(i)].to<JsonArray>();  
    IntArr1.add(MyArray[i].index);
    IntArr1.add(MyArray[i].EffectName);
    IntArr1.add(MyArray[i].Selected);
    }
#endif

#ifdef ExtObject_IntObject
  //External JsonObject, Internal JsonObject 
  JsonObject ExtObj2 = JSON_Obj["ArrayName2"].createNestedObject();  
  for (uint8_t i=0; i<MyArrayCnt; i++) 
    {
    MyStruct s = MyArray[i];
    JsonObject IntObj2 = ExtObj2["ArrayIndex"+String(i)].createNestedObject();
    IntObj2["index"] = s.index;
    IntObj2["EffectName"] = s.EffectName;
    IntObj2["Selected"] =s.Selected;
    }
#endif

  //added more plain variablex
  JSON_Obj["Var3"] = String("after array"); 

  String jsonString;
  //serializeJson(JSON_Obj, jsonString);
  serializeJsonPretty(JSON_Obj, jsonString);
  Serial.println(jsonString);
}

void loop() 
{
}

First approach gives output like this:

{
  "Var2": "before array",
  "ArrayName1": [
    {
      "ArrayIndex0": [
        1,
        "First",
        false
      ],
      "ArrayIndex1": [
        5,
        "Second",
        true
      ],
      "ArrayIndex2": [
        3,
        "Third",
        false
      ]
    }
  ],
  "Var3": "after array"
}

The output of the second one is here:

{
  "Var2": "before array",
  "ArrayName2": [
    {
      "ArrayIndex0": [
        {
          "index": 1,
          "EffectName": "First",
          "Selected": false
        }
      ],
      "ArrayIndex1": [
        {
          "index": 5,
          "EffectName": "Second",
          "Selected": true
        }
      ],
      "ArrayIndex2": [
        {
          "index": 3,
          "EffectName": "Third",
          "Selected": false
        }
      ]
    }
  ],
  "Var3": "after array"
}

I like first one more, less text :-). Now, I need to decode it on the java-script side.
Thanks to all.

Glad you solved it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.