Arduinojson: Detect if object exist?

Created on 20 Feb 2016  路  3Comments  路  Source: bblanchon/ArduinoJson

If read the wiki, but can't find a anwer. I'm feeling I'm overlooking something but can
t find out the correct way.
Let's say I have this simple json:

{
    "object1":
    {
        "key": "val"
    },
    "object2":
    {
        "key": "val"
    }
}

Now, I want to detect if object2 exists at all, before writing to it.
When trying to find out it a object is empty or not:

JsonObject& obj = json[char_object];
if (obj.containsKey(char_item))

This would be false when the key doesn't exist _or_ the entire object doesn't exist. How can I find out if the object even exist before doing a containsKey ?

question

Most helpful comment

char json[] = "{\"object1\":{},\"object2\":{}}";
JsonObject& root = jsonBuffer.parseObject(json);
if (root.containsKey("object2")) {
   Serial.println("Found object2 !");
}

All 3 comments

char json[] = "{\"object1\":{},\"object2\":{}}";
JsonObject& root = jsonBuffer.parseObject(json);
if (root.containsKey("object2")) {
   Serial.println("Found object2 !");
}

Thank you!

You're welcome, thanks for using ArduinoJson.

Don't hesitated to edit the wiki if you think you can improve it.
In particular, feel free to add you projects to Projects using ArduinoJson.
Also, a GitHub star is always appreciated :wink:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nardev picture nardev  路  5Comments

dimityrivanov picture dimityrivanov  路  5Comments

sorokolokarim picture sorokolokarim  路  5Comments

czuvich picture czuvich  路  7Comments

loewekordel picture loewekordel  路  4Comments