JSON nested array synax

Hi.
What is the syntax to create a JSON nested array inside a nested array?
Thanks

With arduinoJson?

Hi J-M-L
I've got myself in a right mess :slight_smile:
I've encoded a JSON string in arduino as follows:

{"Action 0":{"name":"Start a","type":4,"presetIdx":0,"group":0,"isPlaying":false,"delay":208,"loop":false,"cHSVhue":0,"cHSVsat":0,"cHSVval":0,"cRGBred":127,"cRGBgreen":0,"cRGBblue":255},
"Action 1":{"name":"Start b","type":5,"presetIdx":0,"group":0,"isPlaying":false,"delay":208,"loop":false,"cHSVhue":110,"cHSVsat":255,"cHSVval":255,"cRGBred":0,"cRGBgreen":0,"cRGBblue":0},
"Action 2":{"name":"Start c","type":6,"presetIdx":0,"group":0,"isPlaying":true,"delay":208,"loop":false,"cHSVhue":0,"cHSVsat":0,"cHSVval":0,"cRGBred":0,"cRGBgreen":0,"cRGBblue":0},
"Action 3":{"name":"Start d","type":6,"presetIdx":0,"group":0,"isPlaying":true,"delay":208,"loop":false,"cHSVhue":0,"cHSVsat":0,"cHSVval":0,"cRGBred":0,"cRGBgreen":0,"cRGBblue":0}}

I cannot for the life of me work out how to decode it in JS - its making me cry :frowning:
Its arriving at the web page, but I simply cannot parse it correctly. I want to iterate through the 'actions' but I cannot even print the object or get its length.

On the Arduino side you would do


#include <ArduinoJson.h>

char json[] = "{\"Action 0\":{\"name\":\"Start a\",\"type\":4,\"presetIdx\":0,\"group\":0,\"isPlaying\":false,\"delay\":208,\"loop\":false,\"cHSVhue\":0,\"cHSVsat\":0,\"cHSVval\":0,\"cRGBred\":127,\"cRGBgreen\":0,\"cRGBblue\":255},\"Action 1\":{\"name\":\"Start b\",\"type\":5,\"presetIdx\":0,\"group\":0,\"isPlaying\":false,\"delay\":208,\"loop\":false,\"cHSVhue\":110,\"cHSVsat\":255,\"cHSVval\":255,\"cRGBred\":0,\"cRGBgreen\":0,\"cRGBblue\":0},\"Action 2\":{\"name\":\"Start c\",\"type\":6,\"presetIdx\":0,\"group\":0,\"isPlaying\":true,\"delay\":208,\"loop\":false,\"cHSVhue\":0,\"cHSVsat\":0,\"cHSVval\":0,\"cRGBred\":0,\"cRGBgreen\":0,\"cRGBblue\":0},\"Action 3\":{\"name\":\"Start d\",\"type\":6,\"presetIdx\":0,\"group\":0,\"isPlaying\":true,\"delay\":208,\"loop\":false,\"cHSVhue\":0,\"cHSVsat\":0,\"cHSVval\":0,\"cRGBred\":0,\"cRGBgreen\":0,\"cRGBblue\":0}}";


void setup() {

  Serial.begin(115200);
  StaticJsonDocument<1024> doc;
  DeserializationError error = deserializeJson(doc, json);

  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    return;
  }

  for (JsonPair item : doc.as<JsonObject>()) {
    Serial.println("-------------------");
    const char* key = item.key().c_str(); // the Actions
    Serial.print("Key = "); Serial.println(key);

    const char* name = item.value()["name"]; // the Starts
    Serial.print("name = "); Serial.println(name);

    int type = item.value()["type"];
    Serial.print("type = "); Serial.println(type);

    int presetIdx   = item.value()["presetIdx"];
    Serial.print("presetIdx = "); Serial.println(presetIdx);

    int group       = item.value()["group"];
    Serial.print("group = "); Serial.println(group);

    // etc

    // bool isPlaying  = item.value()["isPlaying"];
    // int theDdelay   = item.value()["delay"];
    // bool theLoop    = item.value()["loop"];
    // int cHSVhue     = item.value()["cHSVhue"];
    // int cHSVsat     = item.value()["cHSVsat"];
    // int cHSVval     = item.value()["cHSVval"];
    // int cRGBred     = item.value()["cRGBred"];
    // int cRGBgreen   = item.value()["cRGBgreen"];
    // int cRGBblue    = item.value()["cRGBblue"];
  }
}

void loop() {}

in Javascript you would probably do something like this

const json = '{"Action 0":{"name":"Start a","type":4,"presetIdx":0,"group":0,"isPlaying":false,"delay":208,"loop":false,"cHSVhue":0,"cHSVsat":0,"cHSVval":0,"cRGBred":127,"cRGBgreen":0,"cRGBblue":255},"Action 1":{"name":"Start b","type":5,"presetIdx":0,"group":0,"isPlaying":false,"delay":208,"loop":false,"cHSVhue":110,"cHSVsat":255,"cHSVval":255,"cRGBred":0,"cRGBgreen":0,"cRGBblue":0}, "Action 2":{"name":"Start c","type":6,"presetIdx":0,"group":0,"isPlaying":true,"delay":208,"loop":false,"cHSVhue":0,"cHSVsat":0,"cHSVval":0,"cRGBred":0,"cRGBgreen":0,"cRGBblue":0}, "Action 3":{"name":"Start d","type":6,"presetIdx":0,"group":0,"isPlaying":true,"delay":208,"loop":false,"cHSVhue":0,"cHSVsat":0,"cHSVval":0,"cRGBred":0,"cRGBgreen":0,"cRGBblue":0}}';

const result = JSON.parse(json);

Object.entries(result).forEach((entry) => {
  const [key, value] = entry;
  console.log(`${key}`);

  for (const [k, v] of Object.entries(value)) {
    console.log(`---> ${k}: ${v}`);
  }
});

(showing two ways for iterating through the objects)

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