I have a set of JSON data that has multiple levels of data. Snippet of the JSON (ignore the missing formatting please):
DATA SET A:
"interaction": {
"author": {
"username": "johndoe",
"name": "John Doe"
}
},
"gender": "male"
DATA SET B:
"interaction": {
"author": {
"name": "Jane Doe"
}
},
"gender": "male"
In a single-level I can use:
if record.has_key?('gender')
and that will return a true/false value if the key is present.
If I try to seed data and the key isn’t present, it will throw an error and stop seeding.
My question: How would I check to see if the “username” key exists. Data Set B, for example, doesn’t have a username key and would throw an error, but I can’t figure out how to modify the has_key() command to check for a few levels down.
Thanks for the help.
I’ve decided to work around the
has_keymethod and used a begin/rescue/end.