I need to test whether a nested attribute exists in a JSON object before doing something with it. How can I accomplish this?
I know I can test one level at the time but this seems crazy when is a complex data structure. (Ruby code)
#!/usr/bin/ruby
require 'json'
json_string = '
{
"name" : "Matias",
"age" : 31,
"team" : {
"name" : "Some team",
"size" : 3
}
}
'
json_obj = JSON.parse(json_string)
puts json_obj['name'] if json_obj['fake']['name']