Description

JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject.

If the JsonObject is null/unbound, this function does nothing.

Signatures

void remove(const char* key) const;
void remove(const __FlashStringHelper* key) const;

void remove(const String& key) const;
void remove(const std::string& key) const;
void remove(std::string_view key) const;

void remove(JsonObject::iterator iterator) const;

Arguments

key: the key to remove from the object.

iterator: an iterator returned by JsonObject::begin()

Examples

In the following examples, we will use this JSON document as input:

{
  "survivors": {
    "Coach": {
      "age": 40,
      "sex": "male",
      "occupation": "teacher"
    },
    "Ellis": {
      "age": 23,
      "sex": "male",
      "occupation": "mechanic"
    },
    "Nick": {
      "age": 35,
      "sex": "male",
      "occupation": "retailer"
    },
    "Rochelle": {
      "age": 29,
      "sex": "female",
      "occupation": "associate producer"
    }
  }
}

Example 1: Remove a member with the specified key

Here is how you can remove “Ellis” from the crew:

deserializeJson(doc, input);
JsonObject crew = doc["survivors"];
crew.remove("Ellis");
serializeJsonPretty(object, Serial);

Execute this program and it will print the following to the serial:

{
  "survivors": {
    "Coach": {
      "age": 40,
      "sex": "male",
      "occupation": "teacher"
    },
    "Nick": {
      "age": 35,
      "sex": "male",
      "occupation": "retailer"
    },
    "Rochelle": {
      "age": 29,
      "sex": "female",
      "occupation": "associate producer"
    }
  }
}

Example 2: Remove all members matching a criteria

Here is how you can remove all female characters from the crew:

deserializeJson(doc, input);
JsonObject crew = doc["survivors"];

for (JsonObject::iterator it=crew.begin(); it!=crew.end(); ++it) {
  if (it->value()["sex"] == "female") {
     crew.remove(it);
  }
}

serializeJsonPretty(object, Serial);

Run this program and it will write theses lines to the serial:

{
  "survivors": {
    "Coach": {
      "age": 40,
      "sex": "male",
      "occupation": "teacher"
    },
    "Ellis": {
      "age": 23,
      "sex": "male",
      "occupation": "mechanic"
    },
    "Nick": {
      "age": 35,
      "sex": "male",
      "occupation": "retailer"
    }
  }
}

See also