'Checking if nested element of JSON exists in ruby

I have the following JSON stored in the structure field. I want to check if the key structure[cluster][ID] exists with ruby within a ternary operator.

{
        "ID": "client DEF",
        "cluster": {
            "ID": "cluster 789",
            "flights": 4,
            "profit": 5245,
            "clv": 2364
        },
        "segment": {
            "ID": "segment 876",
            "flights": 2,
            "profit": 2150,
            "clv": 1564
        },
        "node": {
            "xpos": 1,
            "ypos": 2
        }
    }

Example

structure.has_key?(cluster.ID) ? structure["newField"] = "true" :structure["newField"] = "false"

Root level attributes work well with has_key? - I am not able to check exists on a nested level element. Any help is appreciated.



Sources

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

Source: Stack Overflow

Solution Source