Skip to content

How to check if a key exists without silently generating null objects on the path #2041

Description

@Survivor6666

I have a deep nested JSON data model with a lot of optional keys and optional sub- structures.
When trying to read a non-existing optional key in a not yet existing path the library silently creates this path. When parts of the branch are not existing 'null' keys / 'null' structures are silently generated into the JSON model.

Even checking for a key generates the elements on its path:

...
if (jm["Elements"]["Comments"].contains("Comment")
{ /* Key is existing */
   bOk = !FALSE;
}
...

This generates following JSON output:

{
   "Elements": {
      "Comments": null
   }
}

I tried also the 'find' method with the same result. Addressing a non-existing key generates it.
To check each sub-level with 'contain' or 'find' before accessing the next sub-key is not a real option
because the nesting depth is 10 and the model contains a few thousand 1000 optional keys.

Is there any hint how to check if a key exists without checking each key on its path?

Any help is welcome
Christian

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions