i am trying to convert a json object to a ruby object in a format that is easy to work with. i use the json.decode and that converts it to ruby as hashes and arrays like it should. the part i am trying to figure out is a clean way to reorganize the hashes (see my code below). the first problem i have, is if any of the fields are missing from the json object i get the "You have a nil object when you didn''t expect it!" error. the second issue is it is ugly and hard to read. what is a good way to do this? address = ActiveSupport::JSON.decode(address) @address = {} @address[''status''] = address[''Status''][''code''] @address[''submitted_address''] = address[''name''] @address[''full_address''] = address[''Placemark''][0][''address''] @address[''longitude''] = address[''Placemark''][0][''Point''][''coordinates''] [0] @address[''lattitude''] = address[''Placemark''][0][''Point''][''coordinates''] [1] @address[''elevation''] = address[''Placemark''][0][''Point''][''coordinates''] [2] @address[''accuracy''] = address[''Placemark''][0][''AddressDetails''] [''Accuracy''] @address[''country''] = address[''Placemark''][0][''AddressDetails''] [''Country''][''CountryNameCode''] @address[''state''] = address[''Placemark''][0][''AddressDetails''] [''Country''][''AdministrativeArea''][''AdministrativeAreaName''] @address[''county''] = address[''Placemark''][0][''AddressDetails''] [''Country''][''AdministrativeArea''][''SubAdministrativeArea''] [''SubAdministrativeAreaName''] @address[''city''] = address[''Placemark''][0][''AddressDetails''][''Country''] [''AdministrativeArea''][''SubAdministrativeArea''][''Locality''] [''LocalityName''] @address[''street''] = address[''Placemark''][0][''AddressDetails''] [''Country''][''AdministrativeArea''][''SubAdministrativeArea''][''Locality''] [''Thoroughfare''][''ThoroughfareName''] @address[''zip''] = address[''Placemark''][0][''AddressDetails''][''Country''] [''AdministrativeArea''][''SubAdministrativeArea''][''Locality''] [''PostalCode''][''PostalCodeNumber''] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi skud, skud wrote:> the first problem i have, is if any of the fields are missing > from the json object i get the "You have a nil object when > you didn''t expect it!" error.The easy way to address the first problem, in a brute force way, would be to add to each line so that, for example, ...> @address[''status''] = address[''Status''][''code'']becomes... @address[''status''] = address[''Status''][''code''] unless address[''Status''][''code''].nil?> the second issue is it is ugly and hard to read.Yeah ;-)> what is a good way to do this? > > address = ActiveSupport::JSON.decode(address) > @address = {} > @address[''status''] = address[''Status''][''code''] > @address[''submitted_address''] = address[''name''] > @address[''full_address''] = address[''Placemark''][0][''address'']One way would, assuming the JSON structure is static, would be to create a table / model with a couple of columns; an entry for each of your @address attributes and a corresponding ''path'' to the JSON location for each of your @address attributes. The ''origin'' column would contain strings like "address[''Placemark''][0][''address'']" and the code would like something like... address = ActiveSupport::JSON.decode(address) @address = {} address_mappings = Address_mappings.find(:all) address_mappings.each do |this_mapping| @address[this_mapping.destination] = send(this_mapping.origin) end Definitely NOT elegant. What we call down here in Texas, "lipstick on a pig." ;-) Maybe somebody else will chime in to show us both a better way. HTH, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---