'MQTT message is not received because of object in object

Libraries:

<PubSubClient.h>
<ESP8266WiFi.h>
"ArduinoJson.h"

When I send this JSON, the message is received and I can do things with it:

{"topic":"domoticz/out","payload":{"Battery":255,"LastUpdate":"2022-05-13 16:28:39","Level":25,"RSSI":12,"description":"","dtype":"Color Switch","hwid":"3","id":"00082007","idx":7,"name":"test5","nvalue":15,"stype":"RGBWWZ","svalue1":"25","switchType":"Dimmer","unit":1},"qos":1,"retain":false,"_msgid":"5d58bd06fd9025a4"}

But when I send this message (with the Color object in the payload object), nothing happens, the message is not received.

{"topic":"domoticz/out","payload":{"Battery":255,"Color":{"b":33,"cw":76,"g":77,"m":4,"r":187,"t":179,"ww":180},"LastUpdate":"2022-05-13 16:31:13","Level":37,"RSSI":12,"description":"","dtype":"Color Switch","hwid":"3","id":"00082007","idx":7,"name":"test5","nvalue":15,"stype":"RGBWWZ","svalue1":"37","switchType":"Dimmer","unit":1},"qos":1,"retain":false,"_msgid":"10c972aa25ee323b"}

this is the callback function:

 void callback(char *topic, byte *payload, unsigned int length) {
        Serial.print("Message arrived in topic: ");
        Serial.println(topic);
        StaticJsonDocument<1200> doc;
        //Deserialize the JSON document
        // DeserializationError error = 
        deserializeJson(doc, payload);
        int idx = doc["idx"];
        Serial.printf("       Idx: %d",idx);

I think this is because there is a Color object in the object. Is there a way to receive this message?

Photo

edit: I stripped down the JSON, now a message is received in my Arduino. But still I can't use the Color object variables.

{"Color":{"b":107,"cw":76,"g":238,"m":4,"r":227,"t":179,"ww":180},"Level":49,"idx":7,"nvalue":10,"svalue1":"49"}

tried:

int t = doc["Color.t"];
Serial.printf(" t: %d",t);

did not work



Solution 1:[1]

To access an object inside another you need two steps, hence to "brackets

int t = doc["Color"]["t"];
Serial.printf(" t: %d",t);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1