Search code examples
jsonesp8266arduinojson

How to Create Nested Json Using ArduinoJson on ESP8266


I want this Output Json From Esp8266

I am trying with another json lib https://github.com/arduino-libraries/Arduino_JSON this is lightweight library than arduinojson.org

{
  "ID": "0785E7",
  "DEVICE": "SINGLE_RELAY",
  "Public_Name": "Demo Switch",
  "version": 1,
  "payloadVersion": 1,
  "SW": 1,
  "HW": 1,
  "CHIPSIZE": 1048576,
  "CHIPSPEED": 40000000,
  "CHIPMODE": 3,
  "capabilities": [
    {
      "interface": "Demo.Switch",
      "type": "ONOFF",
      "version": "1.0",
      "sync": true,
      "control_pin": 2,
      "MQTT_SWITCH_TOPIC": "on",
      "MQTT_SWITCH_SYNC": "sync"
    },
    {
      "interface": "Demo.Countdown",
      "type": "Countdown",
      "version": "1.0",
      "sync": true,
      "control_pin": 2,
      "MQTT_SWITCH_TOPIC": "s",
      "MQTT_SWITCH_SYNC": "x"
    },
    {
      "interface": "Demo.Schedule",
      "type": "Schedule",
      "version": "1.0",
      "sync": true,
      "control_pin": 2,
      "MQTT_SWITCH_TOPIC": "s",
      "MQTT_SWITCH_SYNC": "x"
    }
  ]
}

Arduino Function

String Device_info()
{

 JSONVar json;
  json["ID"] = ESPID;
  json["DEVICE"] = DEVICE_TYPE;
  json["Public_Name"] = DEVICE_CHANNEL;
  json["version"] = relaystatus1;
  json["payloadVersion"] = RelayPIN;
  json["SW"] = userid;
  json["HW"] = Trigger;
  json["CHIPSIZE"] = rssi_str;
  json["CHIPSPEED"] = "1";;
  String jsonString = JSON.stringify(json);

}

i want to add "capabilities" objectArray in this Like Above json.


Solution

  • The createNestedObject() method can be used to create the capabilities objects inside a nested array.

    The ArduinoJson Assistant suggests the following code:

    const size_t capacity = 
        JSON_ARRAY_SIZE(3) + 3*JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(11);
    
    DynamicJsonDocument doc(capacity);
    
    doc["ID"] = "0785E7";
    doc["DEVICE"] = "SINGLE_RELAY";
    doc["Public_Name"] = "Demo Switch";
    doc["version"] = 1;
    doc["payloadVersion"] = 1;
    doc["SW"] = 1;
    doc["HW"] = 1;
    doc["CHIPSIZE"] = 1048576;
    doc["CHIPSPEED"] = 40000000;
    doc["CHIPMODE"] = 3;
    
    JsonArray capabilities = doc.createNestedArray("capabilities");
    
    JsonObject capabilities_0 = capabilities.createNestedObject();
    capabilities_0["interface"] = "Demo.Switch";
    capabilities_0["type"] = "ONOFF";
    capabilities_0["version"] = "1.0";
    capabilities_0["sync"] = true;
    capabilities_0["control_pin"] = 2;
    capabilities_0["MQTT_SWITCH_TOPIC"] = "on";
    capabilities_0["MQTT_SWITCH_SYNC"] = "sync";
    
    JsonObject capabilities_1 = capabilities.createNestedObject();
    capabilities_1["interface"] = "Demo.Countdown";
    capabilities_1["type"] = "Countdown";
    capabilities_1["version"] = "1.0";
    capabilities_1["sync"] = true;
    capabilities_1["control_pin"] = 2;
    capabilities_1["MQTT_SWITCH_TOPIC"] = "s";
    capabilities_1["MQTT_SWITCH_SYNC"] = "x";
    
    JsonObject capabilities_2 = capabilities.createNestedObject();
    capabilities_2["interface"] = "Demo.Schedule";
    capabilities_2["type"] = "Schedule";
    capabilities_2["version"] = "1.0";
    capabilities_2["sync"] = true;
    capabilities_2["control_pin"] = 2;
    capabilities_2["MQTT_SWITCH_TOPIC"] = "s";
    capabilities_2["MQTT_SWITCH_SYNC"] = "x";
    
    serializeJson(doc, Serial);