bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.73k stars 1.12k forks source link

Delete key in object #1361

Closed Findus76 closed 4 years ago

Findus76 commented 4 years ago

Continued on issue no: #1352 Your example works, thank you very much! The task was to delete all objects where the key was " R " In the objects where key is "W" I want to delete key "R / W" but also "Description"

DeserializationError err = deserializeJson(FunclistDoc, functionList, Listleght);
    if (!err) 
    {
        if(FunclistDoc.size() == 2)
        {
            const char* Function_Type = FunclistDoc["Type"].as<char*>();
            if (Function_Type)
            {
                if(strcmp(FunclistDoc["Type"].as<char*>(), "SystemFunction") == 0)
                {
                    JsonObject Function_object = FunclistDoc["Function list"].as<JsonObject>();
                    if (!Function_object.isNull())
                    {
                        //char* functionName;
                        //const char* RW_type;
                        //uint16_t cnt = 0;
                        uint16_t ListSize;
                        ListSize = Function_object.size();
                        if(ListSize > 0)
                        {
                            //uint16_t index[ListSize];
                            //uint16_t indexPT = 0;
                            bool statusinForLoop = true;
                        //  JsonObject Function = FunclistDoc["Function list"];
                            for (auto it=Function_object.begin(); it!=Function_object.end() && statusinForLoop; ++it) 
                            {
                                  //Avgör om "R/W" finns och är i rätt typ
                                if (it->value()["R/W"] == "R") 
                                {
                                    Serial.println("Remove object");
                                    Function_object.remove(it);
                                }
                                else if(it->value()["R/W"] == "W") 
                                {
                                    //Function_object.remove(it);
                                    Serial.println("Remove part object");
                                //  FunclistDoc.remove("Function list" it->key() "R/W");
                                //  FunclistDoc.remove("Function list" it->key() "Description");
                                }
                                else 
                                {
                                    Serial.printf("R/W can only bee R or W in function %s\r\n", it->key().c_str());
                                    statusinForLoop = false;
                                }
                            }
                            if(statusinForLoop)
                                serializeJsonPretty(FunclistDoc, Serial);

Example of json string before and desired result

{
    "Type": "SystemFunction",
    "Function list": {
        "Relay ch1": {
            "R/W": "W",
            "Position": "EnergyMeter",
            "Head Type": "Relay",
            "Sub Type": "Main Voltage",
            "State": "ON/OFF",
            "Description": "Solidstate max 2A"
        },
        "Relay ch2": {
            "R/W": "R",
            "Position": "EnergyMeter",
            "Head Type": "Relay",
            "Sub Type": "Main Voltage",
            "State": "ON/OFF",
            "Description": "Solidstate max 2A"
        },
        "Relay ch3": {
            "R/W": "R",
            "Position": "EnergyMeter",
            "Head Type": "Relay",
            "Sub Type": "Main Voltage",
            "State": "ON/OFF",
            "Description": "Solidstate max 2A"
        },
        "Relay ch4": {
            "R/W": "W",
            "Position": "EnergyMeter",
            "Head Type": "Relay",
            "Sub Type": "Main Voltage",
            "State": "ON/OFF",
            "Description": "Solidstate max 2A"
        }
    }
}

Expect result

{
    "Type": "SystemFunction",
    "Function list": {
        "Relay ch1": {
            "Position": "EnergyMeter",
            "Head Type": "Relay",
            "Sub Type": "Main Voltage",
            "State": "ON/OFF",
        },
        "Relay ch4": {
            "Position": "EnergyMeter",
            "Head Type": "Relay",
            "Sub Type": "Main Voltage",
            "State": "ON/OFF",
        }
    }
}

Many thanks!

Brg

bblanchon commented 4 years ago

Hi @Findus76,

You can use JsonObject::remove(const char*).

Best regards, Benoit

Findus76 commented 4 years ago

Hi, Benoit

Thanks.

I make it work like this,

for (JsonObject::iterator it=Function_object.begin(); it!=Function_object.end() && statusinForLoop; ++it) 
{
      //Avgör om "R/W" finns och är i rätt typ
        if (it->value()["R/W"] == "R") 
    {
        Serial.println("Remove object");
            Function_object.remove(it);
    }
        else if(it->value()["R/W"] == "W") 
    {
        Serial.println("Remove part object");
        JsonObject removepart = Function_object[it->key()];
        removepart.remove("R/W");
        removepart.remove("Description");
    }
    else 
    {
        Serial.printf("R/W can only bee R or W in function %s\r\n", it->key().c_str());
        statusinForLoop = false;
    }
}

Is that what you think? Or is there a nicer way?

Brg

bblanchon commented 4 years ago

Hi @Findus76,

This looks correct, but you can simplify the following line:

JsonObject removepart = Function_object[it->key()];

into

JsonObject removepart = it->value();

Best regards, Benoit

Findus76 commented 4 years ago

Thanks!